# Programming Languages - Bit and Byte

Updated: 2021-11-19

## Bit

• the most basic unit of computing
• binary: either 0 or 1

### AND(&)

| AND | 0   | 1   |
| --- | --- | --- |
| 0   | 0   | 0   |
| 1   | 0   | 1   |

### OR(|)

| OR  | 0   | 1   |
| --- | --- | --- |
| 0   | 0   | 0   |
| 1   | 0   | 1   |

### XOR(^)

| XOR | 0   | 1   |
| --- | --- | --- |
| 0   | 0   | 0   |
| 1   | 0   | 1   |

### NOT(!)

| NOT |     |
| --- | --- |
| 0   | 1   |
| 1   | 0   |

## Basic Operations

### Bitwise And: &

int bitmask = 0x000F;
int val = 0x2222;

// 2

### Unary bitwise complement: ~

System.out.println(~256);
// -257

## Byte

• 1 byte = 8 bits = 2 hex character
• another name: octet
• historically, 1 byte(8 bits) is used to encode a single character. ASCII uses 7 bits, 256 code points, more than enough for English characters(both lowercase and uppercase). One extra bit can be used as a parity bit.
• now one character may need more than one byte to store, depend on the encoding.
• in C/C++, byte ranges from 0 to 255
• in Java, byte is signed: -127 to 128

## Bit vs Byte

• bit: a single 0 or 1.
• byte: grouped as 8 bits, still naked 0s and 1s, depend on how do you interpret it.

## tebibyte vs terabyte

• terabyte (TB): 10^12 or 1,000,000,000,000 bytes
• tebibyte (TiB)： 2^40 or 1,099,511,627,776 bytes, roughly 1TiB = 1.1TB