I am learning developing in C now. So I have to understand byte and bit.
As I recognized it, I will write down about these things. I hope to help you to understand it.
Bit
A bit represents 0 or 1. It usually used as base-2 number system.
Here is an example table.
Binary | Decimal | Number of bit |
0 | 0 | 1 |
1 | 1 | 1 |
10 | 2 | 2 |
11 | 3 | 2 |
100 | 4 | 3 |
111 | 7 | 3 |
1000 | 8 | 4 |
1010 | 10 | 4 |
1111 | 15 | 4 |
You found that 4 bit can represent from 0 to 15 in decimal.
So it can represents type of 16 number.
By the way hexadecimal is base-16 number system.
In C, it is written with prefix 0x. Let’s add hexadecimal column to above table.
Binary | Decimal | Hexadecimal | Number of bit |
0 | 0 | 0×0 | 1 |
1 | 1 | 0×1 | 1 |
10 | 2 | 0×2 | 2 |
11 | 3 | 0×3 | 2 |
100 | 4 | 0×4 | 3 |
111 | 7 | 0×7 | 3 |
1000 | 8 | 0×8 | 4 |
1010 | 10 | 0xA | 4 |
1111 | 15 | 0xF | 4 |
Byte
Byte usually represents 8 bit.
Can you understand how it can represents decimal range?
I said 4 bit represents from 0 to 15 In previous paragraph.
As 8 bit equals 4 bit add 4 bit, it is calculated by below expression.
16 * 16 = 256
So 8 bit can represent from 0 to 255 in decimal, and type of 256 number.
Let’s expand previous table until 8 bit.
Binary | Decimal | Hexadecimal | Number of bit |
0 | 0 | 0×00 | 1 |
1 | 1 | 0×01 | 1 |
10 | 2 | 0×02 | 2 |
11 | 3 | 0×03 | 2 |
100 | 4 | 0×04 | 3 |
111 | 7 | 0×07 | 3 |
1000 | 8 | 0×08 | 4 |
1010 | 10 | 0×0A | 4 |
1111 | 15 | 0×0F | 4 |
10000 | 16 | 0×10 | 5 |
10001 | 17 | 0×11 | 5 |
11111 | 31 | 0×1F | 5 |
100000 | 32 | 0×20 | 6 |
111111 | 63 | 0×3F | 6 |
1000000 | 64 | 0×40 | 7 |
1111111 | 127 | 0×7F | 7 |
10000000 | 128 | 0×80 | 8 |
11111111 | 255 | 0xFF | 8 |
Mmm, it seems difficult at a glance. But it is easy to understand if you read it little by little.
Let’s look into decimal 64.
Binary | Decimal | Hexadecimal | Number of bit |
1000000 | 64 | 0×40 | 7 |
First I split binary to two parts. I make each part 4 bit.
100 – 0000
The left part is 100. It is represented 0×4 in hexadecimal.
The right part is 0000. It is represented 0×0 in hexadecimal.
So in hexadecimal, it is represented 0×40.
ASCII Table
ASCII table is character code table.
>> ASCII Table and Description
Let’s add this ASCII code column to previous table we made.
Binary | Decimal | Hexadecimal | ASCII character | Number of bit |
0 | 0 | 0×00 | NUL | 1 |
1 | 1 | 0×01 | SOH | 1 |
10 | 2 | 0×02 | STX | 2 |
11 | 3 | 0×03 | ETX | 2 |
100 | 4 | 0×04 | EOT | 3 |
111 | 7 | 0×07 | BEL | 3 |
1000 | 8 | 0×08 | BS | 4 |
1010 | 10 | 0×0A | LF | 4 |
1111 | 15 | 0×0F | SI | 4 |
10000 | 16 | 0×10 | DLE | 5 |
10001 | 17 | 0×11 | DC1 | 5 |
11111 | 31 | 0×1F | US | 5 |
100000 | 32 | 0×20 | Space | 6 |
111111 | 63 | 0×3F | ? | 6 |
1000000 | 64 | 0×40 | @ | 7 |
1000001 | 65 | 0×41 | A | 7 |
1010001 | 97 | 0×61 | a | 7 |
1111111 | 127 | 0×7F | DEL | 7 |
10000000 | 128 | 0×80 | 8 | |
11111111 | 255 | 0xFF | 8 |
Over decimal 127, there seem to be several type of extended ASCII table.
So I didn’t write character.
Let’s look into row of character ‘A’.
Binary | Decimal | Hexadecimal | ASCII character | Number of bit |
1000001 | 65 | 0×41 | A | 7 |
Character ‘A’ is equal to decimal 65, and hexadecimal 0×41.
Character and int in language C
Previous paragraph shows you relationship between character code and decimal and hexadecimal.
I show you simple C code.
#include <stdio.h> int main(int argc, char const *argv[]) { char c, c2; int i, j; c = 'A'; printf("c is %c. character code is %d, binary is %x\n", c, c, c); //output //c is A. character code is 65, binary is 41 i = 97; printf("i is %c. character code is %d, binary is %x\n", i, i, i); //output //i is a. character code is 97, binary is 61 //char to int j = c; printf("j is %c. character code is %d, binary is %x\n", j, j, j); //output //j is A. character code is 65, binary is 41 //int to char c2 = i; printf("c2 is %c. character code is %d, binary is %x\n", c2, c2, c2); //output //c2 is a. character code is 97, binary is 61 return 0; }
Over One Byte
You can understand one byte. But it seems you have to use more than one byte in ordinary programming.
So I am going to write down it on next article.