CS 211 Reference Sheet: Bits and such

Powers of 2

20=160 = 1 220=165=1 Mi=1,024 Ki = 1,048,576
21 = 2 221=2 Mi = 2,097,152
22 = 4 222=4 Mi = 4,194,304
23 = 8 223=8 Mi = 8,388,608
24=161 = 16 224=166=16 Mi = 16,777,216
25 = 32 225=32 Mi = 33,554,432
26 = 64 226=64 Mi = 67,108,864
27 = 128 227=128 Mi = 134,217,728
28=162 = 256 228=167=256 Mi = 268,435,456
29 = 512 229=512 Mi = 536,870,912
210=1 Ki = 1,024 230=1 Gi=1,024 Mi = 1,073,741,824
211=2 Ki = 2,048 231=2 Gi = 2,147,483,648
212=163=4 Ki = 4,096 232=4 Gi = 4,294,967,296
213=8 Ki = 8,192 240=1610=1 Ti=1,024 Gi = 1,099,511,627,776
214=16 Ki = 16,384 248=1612=256 Ti = 281,474,976,710,656
215=32 Ki = 32,768 250=1 Pi=1,024 Ti = 1,125,899,906,842,624
216=64 Ki = 65,536 256=1614=64 Pi = 72,057,594,037,927,936
217=128 Ki = 131,072 260=1615=1 Ei=1,024 Pi = 1,152,921,504,606,846,976
218=256 Ki = 262,144 263=8 Ei = 9,223,372,036,854,775,808
219=512 Ki = 514,288 264=1616=16 Ei = 18,446,744,073,709,551,616
Key:
important! (memorize!) useful/common power of 16
Ki=“kibi-” (“kilo-”) Ti=“tebi-” (“tera-”)
Mi=“mebi-” (“mega-”) Pi=“pebi-” (“peta-”)
Gi=“gibi-” (“giga-”) Ei=“exbi-” (“exa-”)

Digits in binary

0 4 8 0xC 12
0000 0100 1000 1100
1 5 9 0xD13
0001 0101 1001 1101
2 6 0xA 10 0xE14
0010 0110 1010 1110
3 7 0xB11 0xF15
0011 0111 1011 1111

Common Boolean functions

NOT
x ¬x
0 1
1 0
─▷◦─
AND/NAND
x y xy xy
0 0 0 1
0 1 0 1
1 0 0 1
1 1 1 0
═D─
═D◦─
OR/NOR
x y xy xy
0 0 0 1
0 1 1 0
1 0 1 0
1 1 1 0
═)}─
═|}◦─
XOR/XNOR
x y xy xy
0 0 0 1
0 1 1 0
1 0 1 0
1 1 0 1
═))}─
═))}◦─

Important numbers/formulæ

• Bits per nybble: 4
• Bits per byte*: 8
• Values per nybble: 24=16
• Values per byte: 28=256
• Range of an n-bit unsigned integer: 0 to 2n−1
• Range of an n-bit signed two’s-complement integer: −2n−1 to 2n−1−1
*Also “octet”, because really old stuff sometimes had 6-, 7-, or 9-bit bytes.
 

Endianness

Big-endian: 0x12345678 → {0x12,0x34,0x56,0x78}
Little-endian: 0x12345678 → {0x78,0x56,0x34,0x12}

Bit-twiddling shortcuts

• 2n == 1<<n
• x*2n == x<<n
• ⌊x/2n⌋ == x>>n
• ⌈x/2n⌉ == (x+2n-1)>>n
• x/2n, round to nearest == (x+2n−1)>>n
• x%2n == x&(2n-1)
• -x == ~(x-1) == 1+~x
• Round x down to multiple of 2n: x&-2n
• Round x up to multiple of 2n: (x+2n-1)&-2n
• Round x to nearest multiple of 2n: (x+2n−1)&-2n
• Reduce x to 1 or 0: !!x
• All 1 bits: -1, ~0
• All 0 except bottom n bits: (1<<n)-1
• All 1 except bottom n bits: -1<<n
• Test for power of two: !(x&(x-1))
• Test for odd integer: x&1
• Test for multiple of 2n: !(x&(2n-1))
• Test if bits n1,n2,… set: x&(2n|2n|)
• Test if all of bits n1,n2,… set:
(x&(2n|2n|)

Rules for Boolean algebra

¬¬x=x; x∧¬x=0; x∨¬x=1 x''=x; xx'=0; x+x'=1
xy=yx; x∧(yz)=(xy)∧z xy=yx; x(yz)=(xy)z
xy=yx; x∨(yz)=(xy)∨z x+y=y+x; x+(y+z)=(x+y)+z
xy=yx; x⊻(yz)=(xy)⊻z xy=yx; x⊕(yz)=(xy)⊕z
xx=x; x∧1=x; x∧0=0 xx=x; x∙1=x; x∙0=0
xx=x; x∨1=1; x∨0=x x+x=x; x+1=1; x+0=x
xx=0; x⊻1=¬x; x⊻0=x xx=0; x⊕1=x'; x⊕0=x
x∧(yz)=(xy)∨(xz) x(y+z)=xy+xz
x∨(yz)=(xy)∧(xz) x+yz=(x+y)(x+z)
x∧(yz)=(xy)⊻(xz) x(yz)=xyxz
¬(xy)=¬x∨¬y (xy)'=x'+y'
¬(xy)=¬x∧¬y (x+y)'=x'y'
x∧(xy)=x x(x+y)=x
x∨(xy)=x x+xy=x
xy=(x∧¬y)∨(y∧¬x) xy=xy'+yx'