Wednesday, 23 March 2011

Binary Numbers, Binary Arithmetic and Hexadecimal

Computers store numbers in binary, long sequences of 0s and 1s which correspond to low and high voltages.  Each 0 or 1 is known as a Bit, and  8 bits make up a Byte.  If Bytes are grouped together they make up what are known as words.  Unlike denary which works in base 10 where each place is worth 10 times the one to the right, binary works in base 2 because each digit has twice the value of the one to the right.  As such the number 5 in denary would be 101 in binary, because it is (1x1) + (0x2) + (1x4).  If converting a binary number to denary one a similar method is used - so 33 is 100001 because it is (32x1) + (16x0) + (8x0) + (4x0) + (2x0) + (1x1).
  When adding binary numbers a method of 'carrying' digits is used if there is more than one '1' in a place.  As such if there are two 0s the place is 0, a 1 and a 0 would be 1, two 1s would result in a 1 being carried making 10 and three 1s would result in a 1 being carried and a 1 being kept making 11.  For example, if one were to add 1101 and 1010, then 10111 would be the result, as the two 1s in the 8 column would be carried, as 2 x 8 = 16, meaning the next column would contain a 1.  The next column would contain a 0, whilst the other three would contain 1s because there are two subsequent 1s in the first number, and one in the second number - all in different columns.
  Binary multiplication has similarities to denary multiplication, as it is done with respect to place value.  If multiplying only by a 1 in the furthest right column then the number does not change, or if there are more 1s it is added to the total.  On the other hand if a binary number is multiplied by a number with at least two places (therefore one with a value of at least 2) then the number of places after the 1 is added to the end of the binary number as 0s, meaning that it moves up places.  For example, 100 x 101 would be 10000 +100 = 10100.  This is because the 101 has merely moved up two places.
  Negative numbers can also be represented using binary through utilising Two's Complement.  To convert a positive binary number to a negative one the digits are simply inverted, and then 1 is added.  The denary number 7 is 0111 in binary, and this is flipped to become 1000, and then 1 is added to make it 1001.  This method of 'flipping' the bits is used so that computers are able to store negative numbers.  Also worth mentioning are most and least significant bits.  Positive numbers have 0 as the MSB, whilst it is 1 with negative numbers.  Odd numbers have 1 as the LSB, whilst it is 0 with even numbers.
  With this knowledge it is possible to subtract binary numbers.  This is most easily done by turning the number into a negative one and then adding it.  For example, 7 - 5 would become -5 + 7, or 1011 + 0111, with an answer of two in denary, or 0010 in binary.
  Hexadecimal refers to a base 16 number system, which is used for representing numbers shorthand.  Each digit/letter is has a value of 16 times what it would have one place to the right.  It is often favoured over binary because that will have long chains of 0s and 1s, whilst a number's hexadecimal equivalent will be much shorter.  The numbers 0-9 are used in hexadecimal for their respective values in binary, but then the letters A-F are used for the numbers 10 to 15 in denary, and '10' is used for 16 in denary.  An example of this would be the number 38.  If this number is divided by 16 one gets 2 remainder 6, so a 2 is placed in the column with a value of 16x the digit, and a 6 in the column with a value of 1x the digit- so 38 in denary is 26 in hexadecimal. Letters are only generally used for larger numbers - an example being 173.  This is (10 x 16) + 13, making the hexadecimal number AD.
  Lastly decimals may also be represented using binary, with use of a binary point.  Beyond the binary point are values of 2 to negative powers (or 1 over 2 to powers), each being half the value of the one to the left.  For example the number 5.25 would be 101.01 as 101 is 5 in binary, and .01 in binary is zero halves + one quarter.  Although processing is quick using this system, there is a limited range of fractions available.  As such it would be difficult if possible to represent a denary number like 134.62 in binary.

1 comment:

  1. Fantatsic summary - good examples and all the main points. Yep 134.62 would be v tricky to represent in fractional binary!

    You might find this site interesting:

    http://www.exploringbinary.com

    ReplyDelete