Skip to content

Commit cf0598c

Browse files
committed
binary examples
1 parent c2bb56d commit cf0598c

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package benblack86.binary;
2+
3+
public class BasicBinary {
4+
public static void main(String args[]) {
5+
6+
System.out.printf("Capacity of primitives:\n");
7+
System.out.printf(" byte [ 8 bits] min:%s max:%s\n", Byte.MIN_VALUE, Byte.MAX_VALUE);
8+
System.out.printf(" short [16 bits] min:%s max:%s\n", Short.MIN_VALUE, Short.MAX_VALUE);
9+
System.out.printf(" int [32 bits] min:%s max:%s\n", Integer.MIN_VALUE, Integer.MAX_VALUE);
10+
System.out.printf(" long [64 bits] min:%s max:%s\n", Long.MIN_VALUE, Long.MAX_VALUE);
11+
12+
// 0x => hexadecimal (0xF => 15, 0x10 => 16)
13+
// 0b => binary (0b11 => 3)
14+
15+
// i >> x: shift bits in i right by x positions (2 >> 1 => 1)
16+
// i << x: shift bits in i left by x positions (1 << 1 => 2)
17+
18+
System.out.printf("\nPrint binary using method 1:\n");
19+
for(int i = 125; i < 130; i++) {
20+
printBinary(i);
21+
}
22+
printBinary(-100);
23+
24+
System.out.printf("\nPrint binary using method 2:\n");
25+
for(int i = 125; i < 130; i++) {
26+
printBinary2(i);
27+
}
28+
printBinary2(-100);
29+
30+
31+
System.out.printf("\nBinary operations:\n");
32+
printBinary(128); // 00000000000000000000000010000000 (128)
33+
printBinary(~128); // 11111111111111111111111101111111 (-129)
34+
printBinary(-128); // 11111111111111111111111110000000 (-128)
35+
printBinary(~-128); // 00000000000000000000000001111111 (127)
36+
printBinary(128>>>1); // 00000000000000000000000001000000 (64)
37+
printBinary(128>>>2); // 00000000000000000000000000100000 (32)
38+
printBinary(128>>1); // 00000000000000000000000001000000 (64)
39+
printBinary(128>>2); // 00000000000000000000000000100000 (32)
40+
printBinary(-127>>>1);// 01111111111111111111111111000000 (2147483584)
41+
printBinary(-127>>>2);// 00111111111111111111111111100000 (1073741792)
42+
printBinary(-127>>1); // 11111111111111111111111111000000 (-64)
43+
printBinary(-127>>2); // 11111111111111111111111111100000 (-32)
44+
}
45+
46+
public static void printBinary(int i) {
47+
System.out.printf("%12s: ", i);
48+
for(int k = 31; k > -1; k--) {
49+
// need abs to work with negative numbers
50+
System.out.printf("%s", Math.abs(i >> k) % 2);
51+
}
52+
System.out.printf("\n");
53+
}
54+
55+
public static void printBinary2(int i) {
56+
System.out.printf("%12s: ", i);
57+
for(int k = 31; k > -1; k--) {
58+
// shift bit to the most significant position and apply mask
59+
System.out.printf("%s", i >> k & 0b1);
60+
}
61+
System.out.printf("\n");
62+
}
63+
}

src/benblack86/binary/BitSetTest.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package benblack86.binary;
2+
3+
import java.util.BitSet;
4+
5+
public class BitSetTest {
6+
private static final int RUNS = 100000;
7+
private static final int NUM_BITS = 64;
8+
// every other bit set
9+
private static long setMask = 0xAAAAAAAAAAAAAAAAl;
10+
11+
private static long nativeBitTest() {
12+
long bits = 0;
13+
int found = 0;
14+
bits |= setMask;
15+
16+
long start = System.currentTimeMillis();
17+
18+
for (int bit = 0; bit < NUM_BITS; ++bit) {
19+
if (((bits >>> bit) & 0b1) == 0) {
20+
++found;
21+
}
22+
}
23+
24+
System.out.printf("found: %s\n", found);
25+
26+
return System.currentTimeMillis() - start;
27+
}
28+
29+
private static long bitsetBitTest() {
30+
BitSet bits = new BitSet(NUM_BITS);
31+
for (int bit = 1; bit < NUM_BITS; bit += 2) {
32+
bits.set(bit);
33+
}
34+
35+
long start = System.currentTimeMillis();
36+
37+
int found = 0;
38+
for (int i = bits.nextClearBit(0); i < NUM_BITS; i = bits.nextClearBit(i + 1)) {
39+
++found;
40+
}
41+
42+
System.out.printf("found: %s\n", found);
43+
44+
return System.currentTimeMillis() - start;
45+
}
46+
47+
public static void main(String[] args) {
48+
double totalNative, totalBitset = totalNative = 0;
49+
50+
for (double i = 0; i < RUNS; ++i) {
51+
totalNative += nativeBitTest();
52+
totalBitset += bitsetBitTest();
53+
}
54+
55+
System.out.printf("Native: %f BitSet: %f", totalNative / RUNS, totalBitset / RUNS);
56+
}
57+
}

0 commit comments

Comments
 (0)