Binary Binary Number System Binary to Decimal Decimal to Binary Octal and Hexadecimal Binary to Hexadecimal Hexadecimal to Binary Hexadecimal to Decimal Any Number Base to Decimal Decimal to Any Number Base Binary Coded Decimal (BCD) A computer is a “bistable” device A bistable device: ◦ Easy to design and build ◦ Has 2 states: 0 and 1 One Binary digit (bit) represents 2 possible states (0, 1) With 2 bits, 4 states are possible (22 = 4) Bit1 Bit0 State Bit2 Bit1 Bit0 State 0 0 0 1 0 0 1 2 0 0 1 0 1 2 0 1 0 3 1 0 3 0 1 1 4 1 1 4 1 0 0 5 1 0 1 6 1 1 0 7 1 1 1 8 • With 3 bits, 8 states are possible (23 = 8) Go back • With n bits, 2n states are possible From left to right, the position of the digit indicates its magnitude (in decreasing order) ◦ E.g. in decimal, 123 is less than 321 ◦ In binary, 011 is less than 100 A subscript indicates the number’s base ◦ E.g. is 100 decimal or binary? We don’t know! ◦ But 1410 = 11102 is clear Go back Binary is base 2 Example: convert 10110 (binary) to decimal 101102 = 1x24 + 0x23 + 1x22 + 1x21 + 0x20 = 1x16 + 0x8 + 1x4 + 1x2 + 0x1 = 16 + 0 + 4 + 2 + 0 = 22 So 101102 = 2210 Go back Binary is base 2 Example: convert 35 (decimal) to binary Quotient 35 / 2 = 17 17 / 2 = 8 8/2=4 4/2=2 2/2=1 1/2=0 So 3510 = 1000112 Remainder 1 1 0 0 0 1 Go back It is difficult for a human to work with long strings of 0’s and 1’s Octal and Hexadecimal are ways to group bits together Octal: base 8 Hexadecimal: base 16 With 4 bits, there are 16 possibilities Use 0, 1, 2, 3, …9 for the first 10 symbols Use a, b, c, d, e, and f for the last 6 Bit3 Bit2 Bit1 Bit0 Symbol 0 0 0 0 0 0 0 0 1 1 0 0 1 0 2 0 0 1 1 3 0 1 0 0 4 0 1 0 1 5 0 1 1 0 6 0 1 1 1 7 1 0 0 0 8 1 0 0 1 9 1 0 1 0 a 1 0 1 1 b 1 1 0 0 c 1 1 0 1 d 1 1 1 0 e 1 1 1 1 f Go back 01010110101100112 = ? in hex Group into 4 bits, from the right: 0101, 0110, 1011, 00112 Now translate each (see previous table): 01012 => 5, 01102 => 6, 10112 => b, 00112 => 3 So this is 56b316 What if there are not enough bits? ◦ Pad with 0’s on the left Go back • f0e516 = ? in binary • Translate each into a group of 4 bits: f16 => 11112, 016 => 00002, e16 => 11102, 516 => 01012 So this is 11110000111001012 Go back Hexadecimal is base 16 Example: convert 16 (hex) to decimal 1616 = 1x161 + 6x160 = 1x16 + 6x1 = 16 + 6 = 22 So 1616 = 2210 Not surprising, since 1616 = 0001, 01102 ◦ If one of the hex digits had been > 9, say c, then we would have used 12 in its place. Go back From right to left, multiply the digit of the number-to-convert by its baseposition Sum all results Go back Take the decimal number, and divide by the new number base Keep track of the quotient and remainder Repeat until quotient = 0 Read number from the bottom to the top Go back Why not use 4 bits to represent decimal? Let Let Let Let 0000 0001 0010 0011 represent represent represent represent 0 1 2 3, etc. ◦ This is called BCD ◦ Only uses 10 of the 16 possibilities Go back Go back