Verwer Training and Consultancy Ltd Professional Training and Consultancy for the Automation and Control Industry

Bits Binary and Hexadecimal Representation

1. and Bytes The data manipulated by digital computers is coded into a series of binary digits or bits , each being represented by the presence or absence of a voltage or the on/off state of an electronic switch. These two states are normally designated 0 and 1. The data represented can be numbers, text, images, sounds, money, instructions if fact any information you care to imagine. The code used to represent the information is obviously very important and in most cases widely documented standards for information coding are adhered to (except of course where security is important).

A single bit can represent only two possible values of data, or perhaps two possible instructions. To extend its usefulness, the bits are arranged in groups called words (not to be confused with words of text). Every time the word length is increased by one bit the number of possible combinations is doubled. I.e. two bits can represent four different data values and three bits gives eight combinations. Using a three-bit word for example, we could devise the following coding schemes:

Word state Possible Possible Possible

b2 b1 b0 coded number coded colour coded instruction 0 0 0 0 Black Stop 0 0 1 1 Red Move forward 0 1 0 2 Green Move backwards 0 1 1 3 Yellow Move left 1 0 0 4 Blue Move right 1 0 1 5 Magenta Move up 1 1 0 6 Cyan Move down 1 1 1 7 White Error NB These are simply examples of a possible coding scheme.

Note that the bits are numbered b 0, b 1 … b N-1. in computers and PLCs is normally zero-based, i.e. the first bit is b 0.

A word containing 'N' bits can represent n = 2 N different codes. The right-most bit of a word, b0, is often called the Least Significant Bit , LSB and the left-most bit, b N-1, is called the Most Significant Bit , MSB .

Words of eight bits and multiples thereof are widely used in computers. An eight-bit word is called a . 16 bits is strictly called a “16-bit word”, however the term word without any qualifier is widely used to describe 16 bits. Just to confuse matters, we can also have 32-bit words, which are sometimes called long words . Half a byte (i.e. four bits) is amusingly referred to as a .

Exercise: How many different values or codes can be represented in:

i) a nibble, ii) a byte, iii) a word

Bits & Bytes.doc 1 of 4 A. A. Verwer October 2009 Verwer Training and Consultancy Ltd Professional Training and Consultancy for the Automation and Control Industry

2. Binary Representation of Unsigned Integers How are numbers actually represented in digital computers? Ther are several different schemes used, however initially we will consider only unsigned integers , in other words, positive whole numbers.

First, however, a little revision of the number system: Decimal numbers are numbers to base ten , that is each digit has ten possible values, 0 to 9. Multi-digit decimal numbers have digits with weighting of 10 0, 10 1, 10 2..., i.e. units, tens, hundreds etc. For example the decimal number 123 means 3 plus 2 lots of ten, plus one hundred. OK, enough primary school revision! Lets consider other number schemes.

The bits stored in a computer's memory can be considered as representing numbers in a base two or system. The bits have a weighting of 2 0, 2 1, 2 2, 2 3 ..., i.e. units, twos, fours, eights etc. Remember that a byte has 8 bits, so the LSB has a weighting of 1 and the MSB a weighting of 2 7 = 128. A byte can represent binary numbers from 0000 0000 to 1111 1111 (Note the gap between to make the number easier to read).

To convert from binary to decimal we can just add the bits weighted according to their position. For example:

1010 1101 2 = 1 + 4 + 8 + 32 10 + 128 10 = 173 10

When dealing with different base number systems it is normal to place the base as a small subscript after any multi-digit number to avoid confusion.

The maximum unsigned value which can be represented by a byte is 1111 1111 2 = 255 10 (i.e. 2 8-1). Obviously we need to be able to count to larger numbers than this. To extend the available range we must use longer length words by combining bytes. A 16-bit word can 16 represent integers from zero to 2 -1, (i.e. 0 to 65535 10 ). Modern computer software typically allows us to use integers with word lengths of 8, 16 and 32 bytes.

Exercises: a) What is the largest unsigned decimal integer which could be represented by a 32-bit long word? b) How many bits would you need to store an unsigned number up to 1 million?

To convert from decimal into binary we must successively divide by two. Each time we have a remainder from the division then is becomes digit in the binary result, the least significant bit first. For example:

50 10 becomes : 50/2 = 25 remainder 0 (LSB) 25/2 = 12 remainder 1 12/2 = 6 remainder 0 6/2 = 3 remainder 0 3/2 = 1 remainder 1 1/2 = 0 remainder 1 (MSB)

Bits & Bytes.doc 2 of 4 A. A. Verwer October 2009 Verwer Training and Consultancy Ltd Professional Training and Consultancy for the Automation and Control Industry

and so we have 50 10 = 11 0010 2, or using 8 bit representation: 0011 0010 2.

3. Hexadecimal representation Writing down words in binary is very inefficient and prone to error because we normally need so many digits. Base sixteen numbers called hexadecimal or simply hex offer a much more efficient representation, and with a little practice you can quickly learn to change between binary and hex in your head.

One hex digit can represent 0 to 15 10 , the same as four binary digits or one nibble. Unfortunately, we run out of numeric symbols when we get past 9 and so letters are used to represent the remaining hex digits.

Hex digit Binary number Decimal number 0 0000 0 1 0001 1 2 0010 2 3 0011 3 4 0100 4 5 0101 5 6 0110 6 7 0111 7 8 1000 8 9 1001 9 A 1010 10 B 1011 11 C 1100 12 D 1101 13 E 1110 14 F 1111 15

Conversion between hex and binary is very straightforward; just take it one nibble at a time, for example the 16-bit pattern:

1010 1100 0010 0101 2 can be expressed directly as AC25 16

All number systems follow the same basic rules. Therefore to convert from hex to decimal just add the hex digits weighted according to their position. For example:

3 2 1 0 5BF3 16 = 5 ×16 + 11 10 ×16 + 15 10 ×16 + 3 ×16 = 20480 10 + 2816 10 + 240 10 + 3 = 2353910 10

Conversion from decimal to hex is accomplished by successively dividing by sixteen , each remainder becoming a digit in the hex result, least significant digit first. For example:

50 000 10 becomes: 50000/16 = 3125 remainder 0 (LS digit) 3125/16 = 195 remainder 5 195/16 = 12 remainder 3 12/16 = 0 remainder 12 = C 16 (MS digit)

Bits & Bytes.doc 3 of 4 A. A. Verwer October 2009 Verwer Training and Consultancy Ltd Professional Training and Consultancy for the Automation and Control Industry

and so 50 000 10 = C350 16

Adding and subtracting hex numbers is also straightforward, for example:

ABCD 16 E.g. In the first column: 1 2 3 4 16 D+4 = 13 10 +4 10 = 17 10 = 11 16 B E 0 1 16 i.e. 1 carry 1. 1 1

If you get confused, just think of how we add and subtract using decimal numbers.

4. Further Exercises 1. How many different colours can you store in a 24 bit word? [approx. 16.8 ×10 6]

2. Express the following hexadecimal numbers in binary and in decimal:

ACDC, 859A, FFFF, 1000.

[e.g. ACDC 16 = 1010 1100 1101 1100 2 = 44252 10 ]

3. Express the following decimal numbers as 16 bit unsigned integers using hexadecimal notation:

650, 987, 1000, 25. [e.g. 650 10 = 028A 16 ]

When you have done this, add the resulting hex numbers together and convert the result to decimal to check your answer.

4. A byte is used in a simple pick and place robotic system to represent the motion of the arm. The least significant nibble represents the arm speed (0000 2 to 1111 2) and the individual bits in the most significant nibble represent the direction of motion: b 7 = left, b 6 = right, b5 = up and b 4 = down.

a) How many different speeds can be represented by the scheme? [16]

b) Given that the bit pattern 1111 represents full speed (100%) and 0000 represents zero speed (0%), what speed does the bit pattern 1000 represent? What is the slowest speed that the arm can move? [53.3%, 6.7%]

c) Write down the binary pattern and hexadecimal representation that would move the arm up at full speed. [0010 1111 2 = 2F 16 ]

d) What motion would result if we sent the robot controller the byte A7 16 ? [left and up at 46.7% speed]

e) What would you expect to happen if you sent the decimal value 255 to the controller? [nothing, since the arm cannot move both up and down at the same time!]

Bits & Bytes.doc 4 of 4 A. A. Verwer October 2009