CS10110: Number Bases Decimal, Binary & Hexadecimal (and a little bit of Octal..) Laurence Tyler lgt@aber.ac.uk November 2012 The Digital Computer ► Computers 1's and 0's ● (as we know them today) work on and nothing else! ► All commands to the CPU are just strings of these ► All output from the CPU is in strings of these November 2012 CS10110 - Number Bases 2 So we need to understand binary? ► Yes! ● ● To properly understand how a computer works, you have to understand how it represents and uses numbers Every bit of data a computer uses is eventually represented by a binary number ► You ● ● should be able to: Convert from binary to decimal, and vice-versa Do simple sums in binary November 2012 CS10110 - Number Bases 3 Binary ► Otherwise ► Each ● ● 0 or 1 is called a bit (binary digit) The bit is the fundamental unit of information ► We ● known as base 2 can string bits together to form a byte A byte is usually the smallest addressable unit of computer storage Usually 8 bits, eg: 10011101 (sometimes called an 'octet') ► We are not good at reading binary - we have been trained to work with decimal ► But to understand the computer, we need to understand binary November 2012 CS10110 - Number Bases 4 How the decimal system works In decimal (base 10), each higher digit position is worth ten times the next lower one - powers of 10 - and the digits used are 0..9 (0 to 10-1) E.g: what's the value of 201810 in decimal? ☺ ← Most significant digit Least → Units 103 1000's 102 100's 101 10's 100 1's Digit 2 0 1 8 Value 2000 0 10 8 Result: 2000 + 10 + 8 = 2018 November 2012 CS10110 - Number Bases 5 Binary to Decimal In binary, it's just the same - except that each higher digit position is worth twice the next lower one - powers of 2 - and the only digits are 0 and 1 (0 to 2-1) Taking our example: 100111012 ← Most Units significant bit Least → 27 128's 26 64's 25 32's 24 16's 23 8's 22 4's 21 2's 20 1's 1 0 0 1 1 1 0 1 128 0 0 16 8 4 0 1 Bit Value Result: 128 + 16 + 8 + 4 + 1 = 157 November 2012 CS10110 - Number Bases 6 Decimal to Binary One method: try to subtract powers of two, starting with the highest. Each successful subtraction is a 1 bit, each failure is a 0 bit. E.g. to convert 20110 to binary: Remainder 201 73 9 9 9 1 1 1 Subtract 2n -128 -64 -32 -16 -8 -4 -2 -1 Does it go? Yes Yes No No Yes No No Yes Result bit 1 1 0 0 1 0 0 1 Result: 11001001 (read highest to lowest) November 2012 CS10110 - Number Bases 7 Powers of the Number Base Binary 10000000 1000000 100000 10000 1000 100 10 1 November 2012 Decimal Power of 2 128 27 64 26 32 25 16 24 8 23 4 22 2 21 1 20 Decimal Power of 10 1000 103 100 102 10 101 1 100 Successive digit positions are powers of the number base whatever that number is CS10110 - Number Bases 8 But Decimal is clumsy! ► Decimal (base 10) doesn't translate easily in our heads to and from binary ► e.g. in 8 bits (1 byte) we can represent 28 = 256 different numbers ► To know the bit pattern for a byte we would have to convert it, or else learn it by heart ► Instead, we often use another number base that fits better into bytes - Hexadecimal (hex for short) ► Hexadecimal November 2012 is base 16 CS10110 - Number Bases 9 Hexadecimal Digits ► We only have ten symbols to represent numbers, but for hexadecimal we need 16 ► Q: How do we represent the numbers 10 to 15 as single digits? ► A: By convention, we use the letters A to F ► So our counting goes: 0123456789ABCDEF (then 10 11 12 ... 19 1A 1B 1C 1D 1E 1F 20 ...) November 2012 CS10110 - Number Bases 10 Powers of 16 Hex November 2012 Decimal Power of 16 10000 65536 164 1000 4096 163 100 256 162 10 16 161 1 1 160 CS10110 - Number Bases 11 Why is Hex better? ► One byte = 8 bits ► 256 = 28 = 162 = 102.40824 ► 16 = 24 ► This means each four bits can be represented by one hex digit, and one byte is two hex digits ► e.g. 101100102 = B216 Binary Hex November 2012 1011 B A group of 4 bits is often called a "nybble"! 0010 2 CS10110 - Number Bases 12 Octal ► We can - and sometimes do - use other number bases as well ► Octal (base 8) arises at times. You've seen it already in this course: $ chmod 751 myfile $ ls -l myfile -rwxr-x--x 111101001 7 ► Now 5 1 try converting 751 into binary! November 2012 CS10110 - Number Bases 13 Octal beware! ► Many programming languages let you write numbers in different bases in your source code: ● ► In ● ● ● Decimal, Hex, Octal and sometimes binary C, C++ and Java (and possibly others): Decimal - just the digits e.g. 12857 Hexadecimal - 0xB16F or 0xb16f Binary - 0b101010 (if supported) ► BUT NOTE: Any plain "number" that starts with just a '0' is treated as OCTAL, not DECIMAL! ● ● so 017 in your program is not seventeen but fifteen! writing e.g. 019 will get you a compiler error! November 2012 CS10110 - Number Bases 14 Arithmetic in other bases ► It's just like decimal, except you have to remember to "carry" or "borrow" at the right point 1 0 + 1 1 ===== 1 0 1 1 0 1 - 1 1 ===== 1 0 Binary November 2012 3 F + 8 4 ===== C 3 4 3 - 2 E ===== 1 5 Hexadecimal CS10110 - Number Bases 15 Summary ► Make ● ● sure you can: Convert between binary, decimal and hex (without a calculator!) Do simple sums in binary and hex (just addition and subtraction) ► Tip: Learn to recognise the first few powers of 2 (2, 4, 8, 16, 32, 64, 128, 256, 512, 1024...) and powers of 16 (16, 256, 4096, 65536...) rather than having to work them out each time you need them November 2012 CS10110 - Number Bases 16