Bits, Bytes, Bit-level manipulations

Bits:
The smallest unit for computers to store information. A bit is either 0 or 1. The word “bit” is from the combination of “binary digit”.

Bytes:
A byte consists of 8 bits. 256 cases can be represented by a byte.

In binary, 0000 0000 to 1111 1111. Base 2, which means, two symbols are used here to represent numbers. Computers can understand binary clearly, but the reason why we barely use it in our normal life is that it is too long and not straight-forward for human beings to read and write (I guess).

In decimal, 0 to 255. Base 10. Decimal is the most familiar numeral system for humans. This is probably because of people have 10 fingers.

In hexadecimal, 00 to FF. Base 16, from 0 to 9, and then A, B, C, D, E, F. This is because engineers tend to group bits to make work easier. This is kinda like a human-friendly representation of binary-coded values. Similarly we also use octal to group 3 bits in earlier days.

How many bits does each type in C takes up?

type 32 bit 64 bit x86-64
char 1 1 1
short 2 2 2
int 4 4 4
long 4 8 8
float 4 4 4
double 8 8 8
pointer 4 8 8

A 32-bit system can access 32 bit (2^32) memory address. A 64-bit operating system has 64 bit for memory address. An x86-64 is a 64-bit system over x86 instruction set.

Bit-level manipulations:
&: and. True if both of the two factors are true, false otherwise.

& 0 1
0 0 0
1 0 1

|: or. True if at least one of the two factors is true.

| 0 1
0 0 1
1 1 1

~: not. If A = 1, ~A = 0; if A = 0, ~A = 1;

^: xor. True if and only if one of the two factors is true.

^ 0 1
0 0 1
1 1 0

Bit-level manipulations can be used for bit vectors, sets, “intergral” types in C (char, short, int, long, unsigned)..


References:
1. 15213 Course Home
2. Hexadecimal_numeral_system
All of the articles here are just personal reading notes. Please contact me if you believe there is any copyright infringement.