Table of Contents
Description
- Bits are base-2 numbers.
- A single bit can either be 0 or 1.
- A binary number consists of several bits. E.g. 010101011.
- Bit manipulation involves processing a binary number and performing some operation on its individual bits, then outputting a new binary number.
Bits and Bytes
Bits and Space
- A 32-bit number has $2^{32}$ possible values.
- An unsigned 32-bit number ranges from 0 to $2^{32}-1$
- A signed 32-bit number ranges from $-2^{31}$ to $2^{31}-1$ (even split).
- Storing every possible 32-bit number takes $2^{32}$ bits.
- Increasing bit size.
- Adding 1 bit (digit) doubles the space usage and number of possible binary numbers.
- $2^{30}$ is 1Gbits. $2^{31}$ is 2Gbits.
- Multiplying bits by 10 is roughly the same as multiplying space by 1000.
- $2^1$ ≈ 1bit
- $2^{10}$ ≈ 1Kbits.
- $2^{20}$ ≈ 1Mbits
- Bytes
- 1 (20) byte = 8 (23) bits
- To convert from bits to bytes, lower the exponent by 3.
- Size properties are the same.
- $2^{30}$ Bytes = 1GB.
- $2^{31}$ = 2GB
- $2^{40}$ = 1TB
Interpreting Binary Numbers
- A decimal number can be seen as a sum of powers of 10.
- $956 = (9)10^2 + (5)10^1 + (6)10^0$
- A binary number can be seen as a sum of powers of 2.
- $1010 = (1)2^3 + (0)2^2 + (1)2^1 + (0)2^0$
- Shortcut: 1111 is $2^4 - 1 = 15$
Negative Binary Numbers
- Usually, the leftmost bit in a binary number (the most significant bit) is the signed bit.
- The signed bit represents the sign of the number.
- If it’s 1, the number is negative. If it’s 0, it’s positive.
- $1010_2$
- Negative number
- Magnitude = $010_2$
- $0010_2$
- Positive number.
- Same magnitude, $010_2$
Converting Binary-Decimal