Bits and Bytes

bit stand for binary digit, and one byte is an 8-bit group.

ASCII

ASCII stands for American Standard Code for Information Interchange.

This is a 7-bit code, meaning there are 2^7 or 128 available codes. It’s composed of two part, printable character and unprintable character.

Parity

In ASCII, we use 7 bits, and there is one bit unused, just for Parity.

Parity is a simple error-detection method used in digital communication and storage.
We add one extra bit (called the parity bit) to the data so that the total number of 1s (including the parity bit) follows a rule.

Practical Implications

Masking

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>

#define LOWER_MASK 0x0F
#define UPPER_MASK 0xF0

int main(int argc, char* argv[]) {
/* Two 4-bit values stored in one 8-bit variable */
char value = 0xA5;
char lower = value & LOWER_MASK;
char upper = (value & UPPER_MASK) >> 4;

printf("Lower: %x\n", lower);
printf("Upper: %x\n", upper);

return 0;
}

Flags

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>

#define FLAG_1 0x01 // 00000001
#define FLAG_2 0x02 // 00000010
#define FLAG_3 0x04 // 00000100
#define FLAG_4 0x08 // 00001000
// more flags...
#define FLAG_8 0x80 // 10000000

int main(int argc, char* argv[]) {
char flag = 0;

// Set flag1.
flag |= FLAG_1;

// Check if a flag is set.
if(flag & (FLAG_1 & FLAG_2)) printf("flag1 is set!\n");

return 0;
}