64-Bit Bitfield Cheat Sheet

Just as a cheat sheet for me, a 64-Bit Bitfield in Dec and Hex.

Bit Int Hex
1 1 0x1
2 2 0x2
3 4 0x4
4 8 0x8
5 16 0x10
6 32 0x20
7 64 0x40
8 128 0x80
9 256 0x100
10 512 0x200
11 1024 0x400
12 2048 0x800
13 4096 0x1000
14 8192 0x2000
15 16384 0x4000
16 32768 0x8000
17 65536 0x10000
18 131072 0x20000
19 262144 0x40000
20 524288 0x80000
21 1048576 0x100000
22 2097152 0x200000
23 4194304 0x400000
24 8388608 0x800000
25 16777216 0x1000000
26 33554432 0x2000000
27 67108864 0x4000000
28 134217728 0x8000000
29 268435456 0x10000000
30 536870912 0x20000000
31 1073741824 0x40000000
32 2147483648 0x80000000
33 4294967296 0x100000000
34 8589934592 0x200000000
35 17179869184 0x400000000
36 34359738368 0x800000000
37 68719476736 0x1000000000
38 137438953472 0x2000000000
39 274877906944 0x4000000000
40 549755813888 0x8000000000
41 1099511627776 0x10000000000
42 2199023255552 0x20000000000
43 4398046511104 0x40000000000
44 8796093022208 0x80000000000
45 17592186044416 0x100000000000
46 35184372088832 0x200000000000
47 70368744177664 0x400000000000
48 140737488355328 0x800000000000
49 281474976710656 0x1000000000000
50 562949953421312 0x2000000000000
51 1125899906842624 0x4000000000000
52 2251799813685248 0x8000000000000
53 4503599627370496 0x10000000000000
54 9007199254740992 0x20000000000000
55 18014398509481984 0x40000000000000
56 36028797018963968 0x80000000000000
57 72057594037927936 0x100000000000000
58 144115188075855872 0x200000000000000
59 288230376151711744 0x400000000000000
60 576460752303423488 0x800000000000000
61 1152921504606846976 0x1000000000000000
62 2305843009213693952 0x2000000000000000
63 4611686018427387904 0x4000000000000000
64 9223372036854775808 0x8000000000000000

Checking if a bit is set:

// AND: Only return 1 if both bits are 1
// 0011 & 0100 = 0000
// 0111 & 0100 = 0100
isSet = (value & 0x4) == 0x4;
isSet = (value & 0x4) > 0;

Setting a bit:

// OR: If either operand is 1, return 1.
// 0011 | 0100 = 0111
newvalue = oldvalue | 0x4; // |=

Unsetting a bit:

// NOT: Invert the Bits
// ~0100 = 1011
// AND: Return 1 if both bits are 1
// 0011 & 1011 = 0011
// 0111 & 1011 = 0011
newvalue = oldvalue & (~0x4) // &= ~0x4;

Toggling a bit:

// XOR: If both bits are equal, return 0
// 0111 ^ 0100 = 0011
// 0011 ^ 0100 = 0111
newvalue = oldvalue ^ 0x4; // ^=