Last active
May 4, 2017 08:38
-
-
Save hrgdavor/c2c4c06ab9b67d0b2cadb5abe8254594 to your computer and use it in GitHub Desktop.
java Example of generating bit masks with "ones" one the left or on the right // ((1<<x)-1) // (-1<<x)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestBitMask { | |
public static void main(String[] args) throws Exception{ | |
System.out.println("Java integer bit mask example"); | |
System.out.println(); | |
System.out.println("Zeroes on the left and x ones on the right. Formula: ((1<<x)-1)"); | |
for(int x=1; x<32;x++) { | |
System.out.println(x+"\t "+padRight( ((1<<x)-1) ,12) +""+intBitsSpaced(((1<<x)-1))); | |
} | |
System.out.println(); | |
System.out.println("Zeroes on the right and x ones on the left. Formula: (-1<<x) "); | |
for(int x=1; x<32;x++) { | |
System.out.println(x+"\t "+padRight( (-1<<x) ,12) +""+intBitsSpaced(((1<<x)-1))); | |
} | |
} | |
private static String padRight(int i, int count) { | |
StringBuffer buffer = new StringBuffer().append(i); | |
while(buffer.length() < count) buffer.append(' '); | |
return buffer.toString(); | |
} | |
public static String intBitsSpaced(int val) { | |
StringBuffer b = new StringBuffer(37);// 32 bits + 3 spaces | |
for(int i=31;i>=0; i--) { | |
b.append((val & (1<<i)) == 0 ? '0':'1'); | |
if(i>0 && i%8 ==0) b.append(' '); | |
} | |
return b.toString(); | |
} | |
} | |
/* | |
Zeroes on the left and x ones on the right. Formula: ((1<<x)-1) | |
1 1 00000000 00000000 00000000 00000001 | |
2 3 00000000 00000000 00000000 00000011 | |
3 7 00000000 00000000 00000000 00000111 | |
4 15 00000000 00000000 00000000 00001111 | |
5 31 00000000 00000000 00000000 00011111 | |
6 63 00000000 00000000 00000000 00111111 | |
7 127 00000000 00000000 00000000 01111111 | |
8 255 00000000 00000000 00000000 11111111 | |
9 511 00000000 00000000 00000001 11111111 | |
10 1023 00000000 00000000 00000011 11111111 | |
11 2047 00000000 00000000 00000111 11111111 | |
12 4095 00000000 00000000 00001111 11111111 | |
13 8191 00000000 00000000 00011111 11111111 | |
14 16383 00000000 00000000 00111111 11111111 | |
15 32767 00000000 00000000 01111111 11111111 | |
16 65535 00000000 00000000 11111111 11111111 | |
17 131071 00000000 00000001 11111111 11111111 | |
18 262143 00000000 00000011 11111111 11111111 | |
19 524287 00000000 00000111 11111111 11111111 | |
20 1048575 00000000 00001111 11111111 11111111 | |
21 2097151 00000000 00011111 11111111 11111111 | |
22 4194303 00000000 00111111 11111111 11111111 | |
23 8388607 00000000 01111111 11111111 11111111 | |
24 16777215 00000000 11111111 11111111 11111111 | |
25 33554431 00000001 11111111 11111111 11111111 | |
26 67108863 00000011 11111111 11111111 11111111 | |
27 134217727 00000111 11111111 11111111 11111111 | |
28 268435455 00001111 11111111 11111111 11111111 | |
29 536870911 00011111 11111111 11111111 11111111 | |
30 1073741823 00111111 11111111 11111111 11111111 | |
31 2147483647 01111111 11111111 11111111 11111111 | |
Zeroes on the right and x ones on the left. Formula: (-1<<x) | |
1 -2 11111111 11111111 11111111 11111110 | |
2 -4 11111111 11111111 11111111 11111100 | |
3 -8 11111111 11111111 11111111 11111000 | |
4 -16 11111111 11111111 11111111 11110000 | |
5 -32 11111111 11111111 11111111 11100000 | |
6 -64 11111111 11111111 11111111 11000000 | |
7 -128 11111111 11111111 11111111 10000000 | |
8 -256 11111111 11111111 11111111 00000000 | |
9 -512 11111111 11111111 11111110 00000000 | |
10 -1024 11111111 11111111 11111100 00000000 | |
11 -2048 11111111 11111111 11111000 00000000 | |
12 -4096 11111111 11111111 11110000 00000000 | |
13 -8192 11111111 11111111 11100000 00000000 | |
14 -16384 11111111 11111111 11000000 00000000 | |
15 -32768 11111111 11111111 10000000 00000000 | |
16 -65536 11111111 11111111 00000000 00000000 | |
17 -131072 11111111 11111110 00000000 00000000 | |
18 -262144 11111111 11111100 00000000 00000000 | |
19 -524288 11111111 11111000 00000000 00000000 | |
20 -1048576 11111111 11110000 00000000 00000000 | |
21 -2097152 11111111 11100000 00000000 00000000 | |
22 -4194304 11111111 11000000 00000000 00000000 | |
23 -8388608 11111111 10000000 00000000 00000000 | |
24 -16777216 11111111 00000000 00000000 00000000 | |
25 -33554432 11111110 00000000 00000000 00000000 | |
26 -67108864 11111100 00000000 00000000 00000000 | |
27 -134217728 11111000 00000000 00000000 00000000 | |
28 -268435456 11110000 00000000 00000000 00000000 | |
29 -536870912 11100000 00000000 00000000 00000000 | |
30 -1073741824 11000000 00000000 00000000 00000000 | |
31 -2147483648 10000000 00000000 00000000 00000000 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment