You are on page 1of 1

Binary Fundamentals

Powers of Two Two’s Complement


Power Decimal Hexadecimal Power Decimal Hexadecimal
The negation of an n-bit integer is 2 n−1 1000⋯0000 is the finite analog
2 1
2 0x00000002 213
8192 0x00002000 its complement with respect to 2n: 1000⋯0000 of the signless point at infinity
n on the real projective line
2 2
4 0x00000004 214
16,384 0x00004000 x + ( − x ) = 0 mod 2
23 8 0x00000008 215 32,768 0x00008000
24 16 0x00000010 2 16 65,536 0x00010000
Negative Positive
25 32 0x00000020 217 131,072 0x00020000 Numbers Numbers
26 64 0x00000040 218 262,144 0x00040000
7 19
−2 n− 2 1100⋯0000 0100⋯0000 2 n− 2
2 128 0x00000080 2 524,288 0x00080000
28 256 0x00000100 220 1,048,576 0x00100000 High bit = 1 High bit = 0
29 512 0x00000200 221 2,097,152 0x00200000
210 1024 0x00000400 222 4,194,304 0x00400000
−1 = 1111⋯1111 0000⋯0001 = +1
11 23
2 2048 0x00000800 2 8,388,608 0x00800000 Negation mirrors 0 and 1000⋯0000
2 12 4096 0x00001000 224 16,777,216 0x01000000 across the center line 0000⋯0000 are fixed under negation

Logical Complement Logical Identities Floating-Point


Unary Binary
Half precision Single precision s m
value = ( −1) 2 e−127 1 +

NOT ~x = −x − 1 ~(x & y) = ~x | ~y 16-bit floating-point 32-bit floating-point 2 23


x ~x 5 bits 10 bits 8 bits 23 bits
0 1 −x = ~x + 1 ~(x | y) = ~x & ~y
exp e mantissa m exponent e mantissa m
Bitwise NOT 1 0 m

{
s
−~x = x + 1 ~x ^ y sign s value = ( −1) 2 e−15 1 + sign s
~x ~(x ^ y) = 210
~−x = x − 1 x ^ ~y
Double precision s m
value = ( −1) 2 e−1023 1 +
64-bit floating-point 252
Binary Logical Operations 11 bits 52 bits
exponent e mantissa m

AND OR
x y x & y x y x | y sign s
0 0 0 0 0 0
0 1 0 0 1 1
Bitwise AND Bitwise OR Special Floating-Point Value Half Float Double
1 0 0 1 0 1
x & y 1 1 1 x | y 1 1 1 +0.0 0x0000 0x00000000 0x00000000_00000000
+1.0 0x3C00 0x3F800000 0x3FF00000_00000000

NAND NOR
x y ~(x & y) x y ~(x | y) Positive infinity 0x7C00 0x7F800000 0x7FF00000_00000000
0 0 1 0 0 1
Smallest positive normalized value 0x0400 0x00800000 0x00100000_00000000
0 1 1 0 1 0
Not AND Not OR Upper limit of non-integer values 0x6400 0x4B000000 0x43300000_00000000
1 0 1 1 0 0
~(x & y) 1 1 0 ~(x | y) 1 1 0 Largest representable positive value 0x7BFF 0x7F7FFFFF 0x7FEFFFFF_FFFFFFFF

ANDC ORC
x y x & ~y x y x | ~y
0 0 0 0 0 1 Mask Creation
0 1 0 0 1 0
AND with complement OR with complement Formula Operation / Effect Illustration
1 0 1 1 0 1
x & ~y 1 1 0 x | ~y 1 1 1 Create mask for all bits other than lowest 1 bit. 1 0 1 1 1 0 0 0
~x | (x − 1)
000⋯000 becomes 111⋯111.
1 1 1 1 0 1 1 1

XOR XNOR
x y x ^ y x y ~(x ^ y)
Create mask for all bits other than lowest 0 bit. 0 1 1 0 0 1 1 1
0 0 0 0 0 1
x | ~(x + 1)
0 1 1 0 1 0 111⋯111 is unchanged.
1 1 1 1 0 1 1 1
Exclusive OR Exclusive NOR
1 0 1 1 0 0
x ^ y 1 1 0 ~(x ^ y) 1 1 1 Create mask for bits left of lowest 1 bit, inclusive. 1 0 1 1 1 0 0 0
x | −x
000⋯000 is unchanged.
1 1 1 1 1 0 0 0

1 0 1 1 1 0 0 0
Bit Manipulation x ^ −x
Create mask for bits left of lowest 1 bit, exclusive.
000⋯000 is unchanged.
Formula Operation / Effect Illustration 1 1 1 1 0 0 0 0

Clear lowest 1 bit. 1 0 1 1 1 0 0 0 0 1 1 0 0 1 1 1


Create mask for bits left of lowest 0 bit, inclusive.
x & (x − 1) If result is zero, then x is zero or 2k. ~x | (x + 1)
111⋯111 becomes 000⋯000.
000⋯000 is unchanged. 1 0 1 1 0 0 0 0 1 1 1 1 1 0 0 0

Set lowest 0 bit. 0 1 1 0 0 1 1 1 Create mask for bits left of lowest 0 bit, exclusive. 0 1 1 0 0 1 1 1
x | (x + 1) ~x ^ (x + 1)
111⋯111 is unchanged. 111⋯111 becomes 000⋯000.
0 1 1 0 1 1 1 1 1 1 1 1 0 0 0 0

Set all bits to right of lowest 1 bit. 1 0 1 1 1 0 0 0 Create mask for bits right of lowest 1 bit, inclusive. 1 0 1 1 1 0 0 0
x | (x − 1) x ^ (x − 1)
000⋯000 becomes 111⋯111. 000⋯000 becomes 111⋯111.
1 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1

Clear all bits to right of lowest 0 bit. 0 1 1 0 0 1 1 1 1 0 1 1 1 0 0 0


Create mask for bits right of lowest 1 bit, exclusive.
x & (x + 1) If result is zero, then x is zero or 2k − 1. ~x & (x − 1)
000⋯000 becomes 111⋯111.
111⋯111 becomes 000⋯000. 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1

Extract lowest 1 bit. 1 0 1 1 1 0 0 0 Create mask for bits right of lowest 0 bit, inclusive. 0 1 1 0 0 1 1 1
x & −x x ^ (x + 1)
000⋯000 is unchanged. 111⋯111 is unchanged.
0 0 0 0 1 0 0 0 0 0 0 0 1 1 1 1

Extract lowest 0 bit (as a 1 bit). 0 1 1 0 0 1 1 1 Create mask for bits right of lowest 0 bit, exclusive. 0 1 1 0 0 1 1 1
~x & (x + 1) x & (~x − 1)
111⋯111 becomes 000⋯000. 111⋯111 is unchanged.
0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 1

By Eric Lengyel
Copyright © 2023

You might also like