Decimal, binary and hex representations
When working with any kind of digital system in which numbers are being represented, it is important to understand the different ways numbers are represented in these systems.
In electronics, almost without fail, numbers are represented by two voltage levels which can represent a one or a zero. The number system based on ones and zeroes is called the binary system: because there are only two possible digits. In HTML programming, colors can be represented by a 6-digit hexadecimal number: FFFFFF represents white whereas 000000 represents black.
Before discussing the binary and hexadecimal, let's review the decimal system because many of the concepts will be easier to understand when introduced alongside their decimal counterpart.
The decimal numeral system is the most commonly used and the standard system in daily life. It uses the number 10 as its base, also known as radix. Therefore, it has 10 symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. The system gives positions to the digits in a number and uses powers of the base 10; digits are raised to the N power, in accordance with the position.
For example, the number 123.4510 in the decimal system is:
1 is in the position of hundreds: 102 = 100
2 is in the position of tens: 101 = 10
3 is in the position of ones: 100 = 1
4 is in the position of tenths: 10-1 = 0.1
5 is in the position of hundredths: 10-2 = 0.01
So, it can also be represented as:
123.4510 = (1*102)+(2*101)+(3*100)+(4*10-1)+(5*10-2)
Another number representation system is the binary one, each digit in a binary number is called a bit. Each column of a binary number has twice the weight of the previous column, so binary numbers are base 2.
In binary, the column weights (from right to left) are 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536 and so on. If you work with binary numbers often, you’ll save time if you remember these powers of two.
An N-bit binary number represents one of 2N possibilities. With 8-bit we can represent any number between 0 and 255 (28=256 possibilities):
Bit position 7..0
All the decimal numbers we can think of can be represented into binary symbols. We do this by using a sum between terms of the power of 2 multiplied by 0 or 1. For example, the number 101011112 in the binary system can be represented as:
101011112 = (1*27)+(0*26)+(1*25)+(0*24)+(1*23)+(1*22)+(1*21)+(1*20) = 17510
There are a few ways to represent negative binary numbers. The simplest of these methods is called one's complement, where the sign of a binary number is changed by simply toggling each bit (0's become 1's and vice-versa). This has some difficulties, among them the fact that zero can be represented in two different ways (for an 8-bit number these would be 0000 0000 and 1111 1111 corresponding to +0 and -0).
The problems of multiple representations of 0 and the need for the end-around carry are circumvented by a system called two's complement. To represent an N-bit signed binary number the most significant bit (MSB) has a special significance. If MSB is not set the representation of signed and unsigned numbers is the same. However, when MSB is set, the number is always negative. For this reason MSB is sometimes called the sign bit.
The representation of unsigned binary fractions proceeds in exactly the same way as decimal fractions:
0.10102 = (1*2-1)+(0*2-2)+(1*2-3)+(0*2-4) = 0.62510
Each place to the right of the decimal point represents a negative power of 2, just as for decimals they represent a negative power of 10.
Signed binary fractions are formed much like signed integers. Therefore, MSB represents a sign bit just as with two's complement integers. If this bit is set, the number is negative, otherwise the number is positive.
The hexadecimal (HEX) system, uses the number 16 as its base (radix). As a base-16 numeral system, it uses 16 symbols. These are the 10 decimal digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) and the first six letters of the alphabet (A, B, C, D, E, F). The letters are used because of the need to represent the values 10, 11, 12, 13, 14 and 15 each in one single symbol.
HEX is used in mathematics and information technologies as a more friendly way to represent binary numbers. Each hex digit represents four binary digits; therefore, hex is a language to write binary in an abbreviated form.
Four binary digits (also called nibbles) make up half a byte. This means one byte can carry binary values from 0000 0000 to 1111 1111. In HEX, these can be represented from 00 to FF.
The hexadecimal system can express negative numbers the same way as in decimal.