53
Mathematics for IT Binary Number System Unit 26 Lee Chadwick

09 binary number systems

Embed Size (px)

Citation preview

Page 1: 09   binary number systems

Mathematics for IT

BinaryNumberSystem

Unit 26

Lee Chadwick

Page 2: 09   binary number systems

Binary Number System

• Topic Definitions• A Question!• Lesson learning outcomes• Hex Character Range• Binary – Hex Conversion• Hex – Binary Conversion• Hex – Denary Conversion

Page 3: 09   binary number systems

• Hexadecimal: "Hexadecimal" means "based on 16" (From Greek hexa: "six" and Latin decima: "a tenth part").

• Decimal: Based on 10; Example: the numbers we use in everyday life are decimal numbers, because there are 10 of them (0,1,2,3,4,5,6,7,8 and 9).

• Denary:Same as Decimal – Base 10 • Binary: The word binary comes from "Bi-" meaning two. We see "bi-" in

words such as "bicycle" (two wheels) or "binocular" (two eyes). Binary only uses 2 digits; 1 & 0

• Octal: An Octal Number uses only these 8 digits: 0, 1, 2, 3, 4, 5, 6 and 7 Examples:

• 10 in Octal equals 8 in the Decimal Number System.• 167 in Octal equals 119 in the Decimal Number System. Also called Base 8.

Definitions:

Page 4: 09   binary number systems

• Identify the concept of Binary• Establish the purpose of Binary• Compare Base 2, Base 10 and Base 16• Convert Binary to Denary and Back• Multiply Binary values• Divide Binary values

Learning Aims:

Page 5: 09   binary number systems

Have a look at your kettle…• Electronically, binary numbers are

stored/processed using off or on electrical pulses, a digital system will interpret these off and on states as 0 and 1.

• 0 = Off State• 1 = On State

Page 6: 09   binary number systems

Binary Numbers Overview

Binary is a number system used by digital devices like computers, cd players, etc.

Binary is Base 2 unlike our counting system decimal which is Base 10 (denary).

Page 7: 09   binary number systems

LSB versus MSB: Significance

• Binary has only 2 different numerals (0 and 1), unlike Decimal which has 10 numerals (0,1,2,3,4,5,6,7,8 and 9).

Here is an example of a binary number: 10011100

As you can see it is simply a bunch of zeroes and ones, there are 8 numerals in all which make this an 8 bit binary number, bit is short for Binary Digit, and each numeral is classed as a bit.

The bit on the far right (in this case a zero) is known as the Least significant bit (LSB), and the bit on the far left (in this case a 1) is known as the Most significant bit (MSB)

Page 8: 09   binary number systems

Notations used in digital systems:

4 bits = Nibble (1101)

8 bits = Byte (11010010)

16 bits = Word (1100110111001010)

32 bits = Double word (10101111010011101010111010111010)

64 bits = Quad Word (or paragraph) (10101111010011101010111010111010

…………10101111010011101010111010111010)

Page 9: 09   binary number systems

Base 2• When writing binary numbers you will need

to signify that the number is binary (base 2).

• for example take the value 101, as it is written, it would be hard to work out whether it is a binary or decimal (denary) value.

Page 10: 09   binary number systems

Expressing the difference• to get around this problem it is common to

denote the base to which the number belongs by writing the base value with the number, for example:

• 1012 is a binary number and 10110 is a decimal (denary) value.

Page 11: 09   binary number systems

And from there…• Once we know the base then it is easy to

work out the value, for example:

1012 = 1*22 + 0*21 + 1*20 = 5 (five)

10110 = 1*102 + 0*101 + 1*100 = 101 (one hundred and one) (Think of HTU)

Page 12: 09   binary number systems

Sign Bit• One other thing about binary numbers is

that it is common to signify a negative binary value by placing a 1 (one) at the left hand side (most significant bit) of the value, this is called a sign bit, we will discuss this a little later

Page 13: 09   binary number systems

Converting base numbers

Base 2

1010

Base 10

10

Page 14: 09   binary number systems

Converting binary to decimal

• To convert binary into decimal is very simple and can be done as shown below:

Say we want to convert the 8 bit value 10011101 into a decimal value, we can use a formula like that below:

128 64 32 16 8 4 2 11 0 0 1 1 1 0 1

Page 15: 09   binary number systems

Binary to Denary• As you can see we have placed the numbers 1, 2, 4, 8, 16,

32, 64, 128 (powers of two) in reverse numerical order and then written the binary value below.

• To convert you simply take a value from the top row wherever there is a 1 below and add the values together, for instance in our example we would have 128 + 16 + 8 + 4 + 1 = 157. For a 16 bit value you would use the decimal values 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768 (powers of two) for the conversion.

128 64 32 16 8 4 2 11 0 0 1 1 1 0 1

Page 16: 09   binary number systems

Alternatively…

Because we know binary is base 2 then the above could be written as:

1*27 + 0*26 + 0*25 + 1*24 + 1*23 + 1*22 + 0*21 + 1*20 = 157.

128 64 32 16 8 4 2 11 0 0 1 1 1 0 1

Page 17: 09   binary number systems

Converting decimal to binary

• To convert decimal to binary is also very simple.

• You simply divide the decimal value by 2 and then write down the remainder.

• Repeat this process until you cannot divide by 2 anymore, for example let's take the decimal value 157:

Page 18: 09   binary number systems

Remainders…

157 ÷ 2 = 78 with a remainder of 178 ÷ 2 = 39 with a remainder of 039 ÷ 2 = 19 with a remainder of 119 ÷ 2 = 9 with a remainder of 19 ÷ 2 = 4 with a remainder of 14 ÷ 2 = 2 with a remainder of 02 ÷ 2 = 1 with a remainder of 01 ÷ 2 = 0 with a remainder of 1

To convert write this remainder first.

Page 19: 09   binary number systems

Next…• Write down the value of the remainders

from bottom to top (in other words write down the bottom remainder first and work your way up the list)

• Which gives:

10011101 = 157

Page 20: 09   binary number systems

Alternatively…• Divide your first binary value into your

decimal number (157 / 128 = 1 remainder 29) Place the 1 under 128;

128 64 32 16 8 4 2 11 0 0 0 0 0 0 0

• Then divide 29 by 64 = 0• Place a 0 below 64

128 64 32 16 8 4 2 11 0 0 0 0 0 0 0

• Then divide 29 by 32 = 0• Place a 0 below 32

128 64 32 16 8 4 2 11 0 0 0 0 0 0 0

• Then divide 29 by 16 = 1 remainder 13• Place a 1 below 16

128 64 32 16 8 4 2 11 0 0 1 0 0 0 0

• Then divide 13 by 8 = 1 remainder 5• Place a 1 below 8

128 64 32 16 8 4 2 11 0 0 1 1 0 0 0

• Then divide 5 by 4 = 1 remainder 1• Place a 1 below 4

128 64 32 16 8 4 2 11 0 0 1 1 1 0 0

• Then divide 1 by 2 = 0• Place a 0 below 2

128 64 32 16 8 4 2 11 0 0 1 1 1 0 0

• Then divide 1 by 1 = 1 • Place a 1 below 1

128 64 32 16 8 4 2 11 0 0 1 1 1 0 1

Answer =10011101

Page 21: 09   binary number systems

128 64 32 16 8 4 2 11 0 0 0 0 0 0 0

128 64 32 16 8 4 2 11 0 0 0 0 0 0 0

128 64 32 16 8 4 2 11 0 0 0 0 0 0 0

128 64 32 16 8 4 2 11 0 0 1 0 0 0 0

128 64 32 16 8 4 2 11 0 0 1 1 0 0 0

128 64 32 16 8 4 2 11 0 0 1 1 1 0 0

128 64 32 16 8 4 2 11 0 0 1 1 1 0 0

128 64 32 16 8 4 2 11 0 0 1 1 1 0 1

Page 22: 09   binary number systems

Binary Addition

Page 23: 09   binary number systems

Convert these to binary:• 227• 3• 9• 19• 28• 76• 129

Page 24: 09   binary number systems

Adding binary numbers• Adding binary numbers is very similar to

adding decimal numbers, first an example: 0 1 0 1 1 0 1 1

+ 0 0 1 0 0 1 1 1

1 0 0 0 0 0 1 0

1 1 1 1 1 1 1Binary Addition Rules:

0 + 1 = 1

1 + 0 = 1

0 + 0 = 0

1 + 1 = 0 (Carry One)

1 + 1 + Carry = 1 (Carry One)

Page 25: 09   binary number systems

Break Down…• Let's break down the previous example step by step:

1 + 1 = 0 (carry one)1 + 1 (+ the carry) = 1 (carry one)0 + 1 (+ the carry) = 0 (carry one)1 + 0 (+ the carry) = 0 (carry one)1 + 0 (+ the carry) = 0 (carry one)0 + 1 (+ the carry) = 0 (carry one)1 + 0 (+ the carry) = 0 (carry one)

The last carry is placed at the left hand side of the result giving: 10000010

Page 26: 09   binary number systems

Add these:• 0101 + 0010• 01101010 + 01000011• 01010101 + 01111110• Convert to denary and check• Add 01010111 and 10101001 Then Comment upon your result

Page 27: 09   binary number systems

Summary of Last Session:• Discussed binary and it’s importance• Identified binary bit patterns• Converted binary to denary and back• Established rules of binary addition• Started binary addition tasks

Page 28: 09   binary number systems

Today’s Aims• Summarise binary progress• Explore binary subtraction• Discuss and apply “Twos Complement”• Progress onto binary multiplication• Review binary division• Apply binary subtraction and multiplication

rules to set tasks• What was that number?

Page 29: 09   binary number systems

Binary Subtraction

Page 30: 09   binary number systems

Subtracting binary numbers • The most common way of subtracting binary

numbers is done by first taking the second value (the number to be subtracted) and apply what is known as two's complement, this is done in two steps:

1. complement each digit in turn (change 1 for 0 and 0 for 1).

2. add 1 (one) to the result. • note: the first step by itself is known as one's

complement.

Page 31: 09   binary number systems

Positive to Negative• By applying the following steps you are effectively

turning the value into a negative number, and as when dealing with decimal numbers if you add a negative number to a positive number then you are effectively subtracting to the same value.

• In other words 25 + (-8) = 17, which is the same as writing 25 - 8 = 17.

An example, let's do the following subtraction 11101011 - 01100110 (23510 - 10210)

Page 32: 09   binary number systems

Important note:• When subtracting binary values it is

important to maintain the same amount of digits for each number, even if it means placing zeroes to the left of the value to make up the digits.

• For instance in our example we have added a zero to the left of the value 1100110 to make the amount of numerals up to 8 (one byte) 01100110.

Page 33: 09   binary number systems

Ok, here goes…• First we apply two's complement to 01100110

Which gives us: 10011010

0 1 1 0 0 1 1 0 (Reverse zeroes and ones)Step One 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 (Take result and add 1)Step Two + 11 0 0 1 1 0 1 0

1

Page 34: 09   binary number systems

Stage two• Now we need to add 11101011 +

10011010, NOTE: When you do the addition you always disregard the last carry, so our example would be:

1 1 1 0 1 0 1 1+ 1 0 0 1 1 0 1 0

1 0 0 0 0 1 0 11 1 1 1 1 1

Ignore the last carry

Page 35: 09   binary number systems

Our outcome?• which gives us 10000101, now we can

convert this value into decimal, which gives 13310

So the full calculation in decimal is: 23510 - 10210 = 13310 (correct !!)

128 64 32 16 8 4 2 11 0 0 0 0 1 0 1

Page 36: 09   binary number systems

Negative numbers

• The previous example is subtracting a smaller number from a larger number, if you want to subtract a larger number from a smaller number (giving a negative result) then the process is slightly different.

Usually to indicate a negative number the most significant bit (left hand bit) is set to 1 and the remaining 7 digits are used to express the value. In this format the MSB is referred to as the sign bit.

Page 37: 09   binary number systems

The steps involved:1. Apply two's complement to the larger

number. 2. Add this value to the smaller number. 3. Change the sign bit (MSB) to zero. 4. Apply two's complement to value to get

final result. • The most significant bit (sign bit) now

indicates the value is negative.

Page 38: 09   binary number systems

For example: 10010101 - 10110100

(14910 - 18010)

1 0 1 1 0 1 0 0 (Reverse zeroes and ones)Step One 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 (Take result and add 1)+ 10 1 0 0 1 1 0 0

1 1

1 0 0 1 0 1 0 1 (Add this result to the smaller value)Step Two + 0 1 0 0 1 1 0 0

1 1 1 0 0 0 0 11 1 1

Page 39: 09   binary number systems

Moving on…1 1 1 0 0 0 0 1 (Change Sign Bit (MSB) to

zero)Step Three 0 1 1 0 0 0 0 1

0 1 1 0 0 0 0 1 (Reverse zeroes and ones)Step Four 1 0 0 1 1 1 1 0 1 0 0 1 1 1 1 0 (Take result and add 1)+ 11 0 0 1 1 1 1 1

Final result = 10011111 (MSB indicates a negative value)

Page 40: 09   binary number systems

Final outcome:

Now we can convert this value into a negative decimal, which gives -3110

So the full calculation in decimal is 14910 - 18010 = -3110 (correct !!)

Page 41: 09   binary number systems

Try these:

• 11111111 - 00000001• 01010101 - 00101010• 1001 - 1111• 10111000 – 1101 *• 1111 – 01010101 *

*Remember!

Page 42: 09   binary number systems

Binary Multiplication

Page 43: 09   binary number systems

Multiplying binary numbers

• Binary multiplication can be achieved in a similar fashion to multiplying decimal values.

Using the long multiplication method, ie, by multiplying each digit in turn, and then adding the values together.

For example, lets do the following multiplication: 1011 x 111 (decimal 1110 x 710)

Page 44: 09   binary number systems

Regular Multiplication? Easy!

• which gives us 1001101, now we can convert this value into decimal, which gives 7710

So the full calculation in decimal is 1110 x 710 = 7710 (correct !!)

Page 45: 09   binary number systems

Note!• Notice the pattern in the partial products,

as you can see multiplying a binary value by two can be achieved by shifting the bits to the left and adding zeroes to the right.

Page 46: 09   binary number systems

Binary Division

Page 47: 09   binary number systems

Dividing binary numbers • Like multiplication, dividing binary values

is the same as long division in decimal.

For example, lets do the following division: 1001 ÷ 11 (decimal 910 ÷ 310)

Page 48: 09   binary number systems

Division - Further• which gives us 0011, now we can convert

this value into decimal, which gives 310

So the full calculation in decimal is 910 ÷ 310 = 310 (correct !!)

Page 49: 09   binary number systems

FYI:

• note: Dividing a binary value by two can also be achieved by shifting the bits to the right and adding zeroes to the left.

Page 50: 09   binary number systems

CHALLENGE: Due:

• What is the largest binary number that can be held in:– 8 bits– 16 bits– 32 bits– 64 bits

• How many bits make one byte?• What are the possible values that one bit can take?• Convert the decimal numbers 20 and 10 into binary –

Add the binary numbers together - Show your working. Then convert the result back to denary to check your answer.

Page 51: 09   binary number systems
Page 52: 09   binary number systems

0h 1h 2h 3h 4h 5h 6h 7h 8h 9h Ah Bh Ch Dh Eh Fh

0h 0 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240

1h 1 17 33 49 65 81 97 113 129 145 161 177 193 209 225 241

2h 2 18 34 50 66 82 98 114 130 146 162 178 194 210 226 242

3h 3 19 35 51 67 83 99 115 131 147 163 179 195 211 227 243

4h 4 20 36 52 68 84 100 116 132 148 164 180 196 212 228 244

5h 5 21 37 53 69 85 101 117 133 149 165 181 197 213 229 245

6h 6 22 38 54 70 86 102 118 134 150 166 182 198 214 230 246

7h 7 23 39 55 71 87 103 119 135 151 167 183 199 215 231 247

8h 8 24 40 56 72 88 104 120 136 152 168 184 200 216 232 248

9h 9 25 41 57 73 89 105 121 137 153 169 185 201 217 233 249

Ah 10 26 42 58 74 90 106 122 138 154 170 186 202 218 234 250

Bh 11 27 43 59 75 91 107 123 139 155 171 187 203 219 235 251

Ch 12 28 44 60 76 92 108 124 140 156 172 188 204 220 236 252

Dh 13 29 45 61 77 93 109 125 141 157 173 189 205 221 237 253

Eh 14 30 46 62 78 94 110 126 142 158 174 190 206 222 238 254

Fh 15 31 47 63 79 95 111 127 143 159 175 191 207 223 239 255

To c

onve

rt be

twee

n de

cim

al a

nd

hex

is q

uite

sim

ple

with

this

cha

rt:

Page 53: 09   binary number systems

Number Systems – Task:Number Systems Working Together

Using a structured approach, complete a range of conversions between the number systems discussed previously. The conversions needed are:• Decimal to Binary, Octal, Hexadecimal• Binary to Decimal, Octal, Hexadecimal • Octal to Decimal, Binary, Hexadecimal• Hexadecimal to Decimal, Binary, Octal