Binary Numbers CS102 Subrina Thompson Binary Numbers Overview • Binary is a number system used by digital devices like computers, etc. • Binary is a Base 2, unlike your counting system decimal which is Base 10 (denary) • Binary has only 2 different numerals (0, and 1) • Example of a Binary number: 10011100 Notations • Notations used in digital systems: 4 bits = Nibble 8 bits = Byte 16 bits = Word 32 bits = Double Word 64 bits = Quad Word (or paragraph) Notations • 101 is a binary number and 101 is a decimal (denary) value. 2 10 • Once we know the base then it is easy to work out the value, for example: • 101 = 1*22 + 0*21 + 1*20 = 5 (five) • 101 = 1 * 102 + 0*101 + 1*100 = 101 (one hundred and one) 2 10 Converting a Decimal to Binary • Example: Convert 157 to binary. 157÷ 2 = 78 remainder 1 78 ÷ 2 = 39 remainder 0 39 ÷ 2 = 19 remainder 1 19 ÷ 2 = 9 remainder 1 9 ÷ 2 = 4 remainder 1 4 ÷ 2 = 2 remainder 0 2 ÷ 2 = 1 remainder 0 1 ÷ 2 = 0 remainder 1 Next, write down the value of the remainder from bottom to top. 100111012 = 15710 Convert Binary to Decimal • To convert binary into decimal, say 8 bit value 10011101, we can use a formula like that below 128 64 32 16 8 4 2 1 1 0 0 1 1 1 0 1 • To convert, take the top row wherever there is a 1 below and then add the values together. • In the example above, this would give us: 128 + 16 + 8 + 4 + 1 = 157 Binary Addition • Conceptually similar to decimal addition • Example: Add the binary numbers 1010 and 11 (carry) 1 1 0 1 0 + 1 1 1 1 0 1 2’s Complement Notation • 2’s complement representation – widely used in microprocessors • Represents sign and magnitude LSB MSB Sign bit (0 = + ; 1 = -) Decimal +7 +4 +1 0 -1 -4 -7 2’s Complement 0111 0100 0001 0000 1111 1100 1001 Convert Byte to 2’s Complement • Positive number to Negative number • Negative number to Positive number • Step 1: Complement/Flip the bits (1’s • Step 1: Complement/Flip the bits (1’s • Step 2: Add 1 to the number • Step 2: Add 1 to the number complement) • e.g. 001111112 • Step 1: • Step 2: 6310 11000000 11000000 complement) • e.g. 110000012 -6310 • Step 1: • Step 2: 00111110 00111110 + 00000001 + 00000001 ---------- ---------- 11000001 -63 00111111 -63 Convert Byte to 2’s Complement • Note: To know what decimal number a negative byte is, find the 2’s complement. • • • • MAX (positive byte) 01111111 127 = 27 - 1 MIN (negative byte) 10000000 128 = 27 Convert Byte to 2’s Complement • Overflow 127 01111111 2 00000010 ----------------129 10000001 • Overflow – If the high bit (left-most bit result is 1 (negative) and the two numbers are positive, then we have an OVERFLOW • Underflow – opposite of overflow (happens when you add two negative numbers and the left-most bit is a 0 (positive)) • Note: You can’t get an overflow or underflow from adding positive and negative numbers. Biased Notation • To convert from biased to 2’s complement, flip the left-most bit. • e.g. 11111101 biased • 2’s complement 01111101