Monday, June 22, 2009

binary, decimal, and hex numbers

There are three major number systems in network, binary, decimal and hex numbers.
The binary numeral system, or base-2 number system represents numeric values using two symbols, usually 0 and 1.
The decimal number system, or base-10 number system represents numeric values using 10 symbols, 0,1,2,3,4,5,6,7,8,9, which we use everyday.
The hex number system, or base-16 number system represents numeric values using 16 symbols, 0,1,2,...8,9, a, b, c, d, e, f.

For example, a subnet mask 255.255.255.224, can be expressed in binary as 11111111.11111111.11111111.11100000, in decimal as 255.255.255.224 or in hex as ff.ff.ff.e0.

In cyber world, binary and hex reign, owing to their straightforward implementation in digital electronic circuitry using logic gates. Binary and hex are actually much simpler than decimal, if we can cast away our prejudice for these "strangers".

Since both binary and hex are machine friendly, it is very simple to convert between them.
16 = 2^4, so it takes four digits of binary to represent one digit of hexadecimal.

Binary to Hex convert
Given a binary number 11100000, we divide it into 4 digits groups as 1110,0000, then convert each group to its hex counter part, d for 1110 (1111 is f, so 1110 is f minus 1 -- d) and 0 for 0000, thus the answer e0.

Hex to Binary convert
The same applied to hex-binary convert. Given hex number f0, we divide it into two groups as f,0, then convert each group to its binary counter part, f for 1111, 0 for 0000, thus the answer 11110000.

Convert from hex and binary to decimal or vise versa is a little bit harder, because there's no natural relationship between them. (By the way, Arab mathematician Abu'l-Hasan al-Uqlidisi invented the decimal number system. If he picked 16 instead of 10 as the base number from the beginning, he would have saved us zillions of headach!!)

A decimal number 224 have value:
224 = 2*base^2 + 2*base^1 + 4*base^0 = 2*100 + 2*10 + 4, the same rule applied to binary and hex.
Binary to Decimal convert
Given binary number 11100000, we begin from the most significent bit (leftmost), the first 1 is at the eighth bit, thus represents value 1*base^(8-1) = 1*2^7 = 128. The second 1 is at the seventh bit, thus represents value 1*base^(7-1) = 2^6 = 64, the third one 2^5 = 32, the first 0 represents 0*2^4 = 0... Finally the value is 128+64+32+0+0+0....+0 = 224.

Hex to Decimal convert
Given hex number e0, we begin from the most significent bit (leftmost), the first e is at the second bit, thus represents value e*base^(2-1) = 14*16^1 = 224. The second 0 is at the first bit, thus represents value 0*base^(1-1) = 0*16^0 = 0. Finally the value is 224+0 = 224.

Decimal to Binary convert
Short division by two with remainder, it relies only on division by two.
Given decimal number 224, write the decimal number as the dividend inside an upside-down "long division" symbol. Write the base of the destination system (in our case, "2" for binary) as the divisor outside the curve of the division symbol.
2)224
_______

Write the integer answer (quotient) under the long division symbol, and write the remainder (0 or 1) to the right of the dividend.
2)224 0
_______
112
Continue downwards, dividing each new quotient by two and writing the remainders to the right of each dividend. Stop when the quotient is 1.
2)224 0
_______
2)112 0
_______
2 )56 0
_______
2 )28 0
_______
2 )14 0
_______
2 ) 7 1
_______
2 ) 3 1
_______
2) 1 1
_______
2) 0 0
_______

Starting with the bottom 1, read the sequence of 1's and 0's upwards to the top. You should have 11100000. This is the binary equivalent of the decimal number 224.

Hex to Binary convert
Short division by 16 with remainder, it relies only on division by 16.
Given decimal number 224, write the decimal number as the dividend inside an upside-down "long division" symbol. Write the base of the destination system (in our case, "16" for hex) as the divisor outside the curve of the division symbol.
16)224
_______
Write the integer answer (quotient) under the long division symbol, and write the remainder (0 to f) to the right of the dividend.
16)224 0
_______
14
Continue downwards, dividing each new quotient by 16 and writing the remainders to the right of each dividend. Stop when the quotient is less than 16.
16)224 0
_______
16) 14 e
_______
Starting with the bottom, read the sequence of digits upwards to the top. You should have e0. This is the hex equivalent of the decimal number 224.

[ Socialize This]

No comments:

Post a Comment

Why I stopped publishing blog posts as information provider

Now the AI can generate content. Does that mean the web publishing industry reaches the end? ChatGPT said: ChatGPT Not at all. While AI can ...