1 Bit (b) = 1 Bit
1 Nibble = 4 Bits
1 Byte (B) = 8 Bits
1 Kilobyte (KB) = 1024 Bytes
1 Megabyte (MB) = 1024 Kilobytes
1 Gigabyte (GB) = 1024 Megabytes
1 Terabyte (TB) = 1024 Gigabytes
1 Petabyte (PB) = 1024 Terabytes
1 Exabyte (EB) = 1024 Petabytes
1 Zettabyte (ZB) = 1024 Exabytes
1 Yottabyte (YB) = 1024 Zettabytes
Memory Size Table
Unit | Size (Bytes) |
---|---|
Bit (b) | 1 bit or 1/8 byte |
Nibble | 4 bits |
Byte (B) | 8 bits |
Kilobyte (KB) | 1024 bytes |
Megabyte (MB) | 1024 KB |
Gigabyte (GB) | 1024 MB |
Terabyte (TB) | 1024 GB |
Petabyte (PB) | 1024 TB |
Exabyte (EB) | 1024 PB |
Zettabyte (ZB) | 1024 EB |
Yottabyte (YB) | 1024 ZB |
Addition of Bits
Example:
You’re adding:
00110111 (which is 55 in decimal)
+ 00000001 (which is 1 in decimal)
------------
= 00111000 (which is 56 in decimal)
Let’s Add Bit by Bit (Right to Left):
Bit position: 7 6 5 4 3 2 1 0
0 0 1 1 0 1 1 1 ← 00110111
+ 0 0 0 0 0 0 0 1 ← 00000001
-------------------
0 0 1 1 1 0 0 0 ← 00111000
Explanation of the rightmost bits:
1 + 1
=10
→ you write down 0, carry over 11 + 0 + carry 1
=10
→ write down 0, carry 11 + 0 + carry 1
=10
→ write down 0, carry 10 + 0 + carry 1
=1
→ write 1, carry 0- Remaining bits add up normally.
Decimal to Binary Conversion
To convert a decimal number to binary:
- Divide the number by
2
. - Record the remainder.
- Repeat the process with the quotient until you reach
0
. - The binary representation is the remainders read in reverse order.
Example: Convert 13
to binary.
Division Step | Quotient | Remainder |
---|---|---|
13 / 2 | 6 | 1 |
6 / 2 | 3 | 0 |
3 / 2 | 1 | 1 |
1 / 2 | 0 | 1 |
Binary of 13: 1101
Binary to Decimal Conversion
To convert a binary number to decimal:
- Multiply each bit by
2
raised to its position (from right, starting at0
). - Sum the results.
Example: Convert 1101
to decimal.
Decimal of 1101: 13
Decimal to Hexadecimal Conversion
💡 Hex Table (0–15):
Decimal | Hex |
---|---|
0 | 0 |
1 | 1 |
… | … |
10 | A |
11 | B |
12 | C |
13 | D |
14 | E |
15 | F |
To convert a decimal to hexadecimal:
- Divide the decimal number by 16 till Quotient < 16.
- Convert Quotient and Reminders values to hex digits.
- Put them together.
Example: Convert 65 to Hexadecimal.
Step 1:
- 65 ÷ 16 = 4 remainder 1
This gives:
- Quotient = 4
- Remainder = 1
Step 2:
- Quotient:
4
→ hex digit is4
- Remainder:
1
→ hex digit is1
Step 3:
- So, 65 in decimal = 0x41 in hex
Exercise:
-
Convert 10 to hexadecimal. → Answer
-
Convert 15 to hexadecimal. → Answer
-
Convert 31 to hexadecimal. → Answer
-
Convert 64 to hexadecimal. → Answer
-
Convert 127 to hexadecimal. → Answer
-
Convert 255 to hexadecimal. → Answer
-
Convert 1023 to hexadecimal. → Answer
-
Convert 4096 to hexadecimal. → Answer
-
Convert 12345 to hexadecimal. → Answer
-
Convert 65535 to hexadecimal. → Answer
Hexadecimal to Decimal Conversion
To convert a hexadecimal number to decimal, follow this simple method:
- Write the hexadecimal number.
- Replace each hex digit with its decimal equivalent.
- Multiply each by 16 raised to its positional power.
- Add all the products together.
Take each digit, multiply it by 16 raised to the power of its position (counting from right to left, starting at 0), and sum the results.
Example: Convert 2F
to decimal
= (2 × 16¹) + (F × 16⁰)
= (2 × 16) + (15 × 1)
= 32 + 15
= 47
Example: Convert 3A7
to decimal
= (3 × 16²) + (A × 16¹) + (7 × 16⁰)
= (3 × 256) + (10 × 16) + (7 × 1)
= 768 + 160 + 7
= 935
Exercise:
-
Convert hexadecimal A to decimal.
-
Convert hexadecimal 1F to decimal.
-
Convert hexadecimal 3C to decimal.
-
Convert hexadecimal 7E to decimal.
-
Convert hexadecimal FF to decimal.
-
Convert hexadecimal 100 to decimal.
-
Convert hexadecimal 1A3 to decimal.
-
Convert hexadecimal 2F7 to decimal.
-
Convert hexadecimal 3E8 to decimal.
-
Convert hexadecimal FFFF to decimal.
Converting Unsigned Integer to Signed Integer
Not all integer values are positive. In some scenarios, negative integers are required. For Example, to represent the difference between two integers, you need to take into account that the difference could be negative, and only signed integers can hold negative values.
So to represent signed integer in native integer value which can interpret ate by CPU, there is a concept called two’s complement. Which helps in conversion between unsigned and signed values.
MSB → Most Significant Bit
LSB → Least Significant Bit
- Simple sign detection: MSB (Most Significant Bit) is the sign bit:
0
= positive1
= negative
4-bit Example:
Positive Number (e.g., +5):
- Binary:
0101
Negative Number (e.g., -5):
- Start with +5:
0101
- Flip the bits:
1010
- Add 1:
1010
+0001
=1011
- So, -5 =
1011
in 4-bit two’s complement.
Binary | Decimal |
---|---|
0000 | 0 |
0001 | 1 |
0010 | 2 |
0011 | 3 |
0100 | 4 |
0101 | 5 |
0110 | 6 |
0111 | 7 |
1000 | -8 |
1001 | -7 |
1010 | -6 |
1011 | -5 |
1100 | -4 |
1101 | -3 |
1110 | -2 |
1111 | -1 |
- For n bits, two’s complement can represent integers in the range:
- For 8 bits: -128 to +127
- For 4 bits: -8 to +7
8-bit Example:
Positive Number (e.g., +123):
- Binary:
01111011
Negative Number (e.g., -123):
- Start with +123:
01111011
- Flip the bits:
10000100
- Add 1:
10000100
+00000001
=10000101
refer to [[Memory Size#]]
- So, -123 =
10000101
in 8-bit two’s complement.
Let’s decode
10000101
as signed two’s complement:
Break down the bits:
1 0 0 0 0 1 0 1
Bit weights (signed):
- MSB =
- The rest:
Now add the weights for the 1s:
-128
(from the MSB)4
and1
from the last two bits
−128+4+1=−123-128 + 4 + 1 = -123
7-bit integer encoding
It is also known as Base-127 Varint encoding
Chunk Position | Continuation Bit (MSB) |
---|---|
First (LSB side) | 1 if more follow |
Last (MSB side) | Always 0 |
Value Range | Bytes Used | MSB of Each Byte |
---|---|---|
0 – 127 | 1 byte | 0xxxxxxx (MSB = 0) |
128 – 16383 | 2 bytes | First: 1xxxxxxx , Second: 0xxxxxxx |
>16383 | 3+ bytes | All but last: 1xxxxxxx , Last: 0xxxxxxx |
- Convert 300 to binary.
- Break into 7-bit chunks.
- Add continuation bits
Example: Encoding integer 300
Step 1: Convert 300 to binary.
00000001
00101100
→ 16-bit- =
0b100101100
Step 2: Break into 7-bit chunks.
00
0000010
0101100
- chunk =
0101100
- chunk =
0000010
Step 3: Add continuation bits
Converting chunks to 8-bit and then adding 1
to MSB, to the chunk who follows more bytes and 0
to MSB, to the chunk who follows least bytes.
chunk = 0101100
= 00101100
chunk = 0000010
= 00000010
Adding 1
to chunks MSB and 0
to chunks MSB.
chunk = 00101100
+ 1
= 10101100
= 0xAC
→ Hexadecimal
chunk = 00000010
+ 0
= 00000010
= 0x02
→ Hexadecimal
Result:
Encoded as bytes:
[0xAC, 0x02]
Or in binary:
10101100 00000010