Hexadecimal Numbers

Two other forms of number representation can be used to represent integers. They are Octal and Hexadecimal (Hex for short) systems. The first has a base of 8, the second a base of 16. Click Octal number representation to learn about the Octal system.

Hexadecimal

Hexadecimal is 16 based and uses the digits 0 to 9 and the letters a to f to represent the values 0 to 15. Letters are used after values past nine as two digits can not be used. Hexadecimal is used more often than octal as any four bits of a binary number can be converted into its hexadecimal equivalent and vice-versa. The number 345 in hexadecimal is 837 as a decimal.

 162 = 256
 161 = 16
 160 = 1
 
 3  4  5  
 3x256 = 768
 4x16 = 64
 5x1 = 5
 837 (decimal)

Another example using 2 bits is d9, which is 217 in decimal (Note: d in hexadecimal = 13 in decimal)

 161 = 16
 160 = 1
 
 d  9  
 13x16 = 208
 9x1 = 9
 217 (decimal)

An eight bit binary number can be converted into its hexadecimal equivalent easily. For example, convert 11111111 into hexadecimal. (The answer is ff).
  1. Break the number into two lots of four bits: 1111 1111
  2. Convert each lot into its decimal equivalent: 1111 => 15, 1111 => 15
  3. Convert these into hex: 15 => f, 15 => f.
  4. Concatenate them: f and f => ff.
Hexadecimal works well with binary because 4 bits is half of a byte and bytes are one of the basic binary measuring units. E.g. Kilobytes, Megabytes, Gigabytes. Hexadecimal is often used as a more concise way of expressing a large binary number. The new Internet Protocol (version 6) system uses 128 bits and hexadecimal is used to abbreviate this very large number. Also, MAC numbers for internet adapters use 48 bits and are expressed in hexadecimal. An example MAC address is C4 46 19 73 49 70. To see the IP address and MAC number of a Windows computer open a command prompt and type ipconfig /all, then Enter. The MAC number is the physical address reading - see below.



Computer monitors use a red, green and blue colour system.This is also know as RGB for short. Each pixel on the screen is a combination of different strengths of each of the three colours. In a 24 bit colour system a byte, 8 bits (24/3 = 8), is used for each colour. The maximum value a byte can hold id 28 - 1 = 255 (take 1 away to allow for zero). A value of 255, or ff in hex, means the colour is used at its full strength. A value of 0, or 00 in hex, means none of the colour is used in the overall mix of colours. Note: upper or lower case letters can be used when writing hex, so ff is equivalent to FF.

In HTML red, green and blue colour values are often expressed in hexadecimal. There are six hex numbers in a colour; two for each colour. Reading from the left the first two represent the strength/amount of red in the colour, the next two the strength of the green and the last two the strength of the blue.

For example, to set the background colour of a web page to black the following cascading style sheet (CSS) code could be used. Note: the # sign signifies to the web browser that the number is a hexadecimal value.

body{
    background-color:#000000;
}

black colour

In other words no red, green or blue is used, hence the page is black. If we wanted a full strength red the code would be,

body{
    background-color:#ff0000;
}

red colour

If we wanted magenta we would use,

body{
    background-color:#ff00ff;
}

  magenta colour

That is full strength red and blue, and no green.

2563 or 16,777,216 (ffffff in hex) different colour combinations can be made, because there can be 256 shades of each of the three colours. This is known as a 24 bit colour system as 256 in decimal is the maximum value eight bits can represent in binary, and 3 (the number of colours) x 8 = 24.

Additional resources

  • For more information and actual colour values follow this link: HTML colours


Color Color HEX Color RGB
  #000000 rgb(0,0,0)
  #FF0000 rgb(255,0,0)
  #00FF00 rgb(0,255,0)
  #0000FF rgb(0,0,255)
  #FFFF00 rgb(255,255,0)
  #00FFFF rgb(0,255,255)
  #FF00FF rgb(255,0,255)
  #C0C0C0 rgb(192,192,192)
  #FFFFFF rgb(255,255,255)