## [C] Learning Byte and Bit in Language C

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 0x0 1 1 1 0x1 1 10 2 0x2 2 11 3 0x3 2 100 4 0x4 3 111 7 0x7 3 1000 8 0x8 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 0x00 1 1 1 0x01 1 10 2 0x02 2 11 3 0x03 2 100 4 0x04 3 111 7 0x07 3 1000 8 0x08 4 1010 10 0x0A 4 1111 15 0x0F 4 10000 16 0x10 5 10001 17 0x11 5 11111 31 0x1F 5 100000 32 0x20 6 111111 63 0x3F 6 1000000 64 0x40 7 1111111 127 0x7F 7 10000000 128 0x80 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 0x40 7

First I split binary to two parts. I make each part 4 bit.
100 – 0000
The left part is 100. It is represented 0x4 in hexadecimal.
The right part is 0000. It is represented 0x0 in hexadecimal.
So in hexadecimal, it is represented 0x40.

### ASCII Table

ASCII table is character code table.
>> ASCII Table and Description

 Binary Decimal Hexadecimal ASCII character Number of bit 0 0 0x00 NUL 1 1 1 0x01 SOH 1 10 2 0x02 STX 2 11 3 0x03 ETX 2 100 4 0x04 EOT 3 111 7 0x07 BEL 3 1000 8 0x08 BS 4 1010 10 0x0A LF 4 1111 15 0x0F SI 4 10000 16 0x10 DLE 5 10001 17 0x11 DC1 5 11111 31 0x1F US 5 100000 32 0x20 Space 6 111111 63 0x3F ? 6 1000000 64 0x40 @ 7 1000001 65 0x41 A 7 1010001 97 0x61 a 7 1111111 127 0x7F DEL 7 10000000 128 0x80 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 0x41 A 7

Character ‘A’ is equal to decimal 65, and hexadecimal 0x41.

### 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.

Tags: , ,

### One Response to “[C] Learning Byte and Bit in Language C”

1. Shahzad says:

you book sucks. The reason it’s 8 bits to a byte has to do with loacigl space. When you do a calculation you need someplace to put those numbers, hence memory. So, memory must be able to handle binary sets that make up numbers. That 1 or 0 only needs one place in memory so we call it a bit. Which stands for binary digit. Early machines were 8 bit machines, (that would be a hardware limitation) so 8 bits make up a byte. each byte is made up of 2 nibbles, each nibble is 4 bits.