Basics - Bit and Byte

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

Basic Operations

Bitwise And: &

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

System.out.println(val & bitmask);
// 2

Bitwise exclusive OR: ^

Bitwise inclusive OR: |

Unary bitwise complement: ~

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

Signed left shift: <<

Signed right shift: >>

Unsigned right shift: >>>

Byte

  • 1 byte = 8 bits
  • 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

Summary

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