Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 3: Data Binary Arithmetic.

Similar presentations


Presentation on theme: "Topic 3: Data Binary Arithmetic."— Presentation transcript:

1 Topic 3: Data Binary Arithmetic

2 Data: Binary Arithmetic
Like denary numbers, we can add, subtract, multiply, and divide binary numbers We’ll look at addition and subtraction We’ll start simple with addition It works exactly like adding denary numbers We work from the least to the most significant digits, and add the digits on the same column Data: Binary Arithmetic

3 Binary Arithmetic (Addition)
Here’s an example Here we’re adding to 00110 We work our way, from the right to the left, adding the digits Any time there’s too much we carry it over to the next digit Essentially, long addition! 1 + = Carries Data: Binary Arithmetic

4 Binary Arithmetic (Addition)
We can always check our work afterwards Convert binary numbers to denary Including our answer Add up denary numbers See if the two answers match 1 + = Carries → 1110 → 610 = 17 → 1710 Same answer, therefore we are correct! Data: Binary Arithmetic

5 Binary Arithmetic (Addition)
There is a problem with adding binary numbers In a computer, numbers usually have a set size A set number of digits it can use Sometimes adding two numbers could make a number larger than the set size 1 + = 1? Carries Data: Binary Arithmetic

6 Binary Arithmetic (Addition)
This is called an overflow If we don’t have a set size, we can add the 1 as the most significant digit If we do have a set size, we ignore it, ending up with an answer that isn’t accurate 1 + = 1? Carries Set Size No Set Size Answer: Answer: In Denary: 1 In Denary: 33 Data: Binary Arithmetic

7 Data: Binary Arithmetic
Add the following binary numbers Show the answer in binary as well If No Set Size, include the overflow If Set Size, ignore the overflow ANSWERS From top-left to bottom-right: No Set Size Set Size (4 bits) Data: Binary Arithmetic

8 Data: Binary Arithmetic
Binary Logic If you’ve programmed before, you may recognise these operators They handle logic AND – both values need to be true OR – either value needs to be true We can do similar things, bit to bits Rather than booleans These operations are called bitwise AND, and bitwise OR bitwise AND: & bitwise OR: | && AND | | OR Data: Binary Arithmetic

9 Data: Binary Arithmetic
Binary Logic (AND) Let’s say we have two binary numbers Performing an AND on them gives us the following: Can you tell what’s happened? 1 AND = Can you tell what’s happened? We’ve created a new binary number where we’ve only kept the 1’s that appear in the same digit in both operands (numbers in the operation). Data: Binary Arithmetic

10 Data: Binary Arithmetic
Binary Logic (AND) We go through both numbers Digit by digit For each resulting digit, we write: 0 if either of the digits is 0 1 if both of the digits is 1 Uses In networking, we have a subnet mask. It defines what part of an Internet Protocol (IP) address we are interested in. If we AND the subnet mask with the IP address (essentially a long binary number), the only 1’s that will be left are the ones we are looking for. For example: Address: Subnet Mask: AND Result: See how the only 1’s left are the ones also in the subnet mask? Num1 1 Num2 Result Data: Binary Arithmetic

11 Data: Binary Arithmetic
Perform a bitwise AND on the following numbers If the numbers are in binary, output the answer as binary If the numbers are in denary, output the answer as denary You will need to convert the numbers to binary to perform the bitwise AND ANSWERS From top-left to bottom-right: 0 1 0 1101 & 0101 0001 & 1010 0000 & 1111 12 & 2 1 & 3 5 & 10 Data: Binary Arithmetic

12 Data: Binary Arithmetic
Binary Logic (OR) Let’s take the same numbers from before Performing an OR on them gives us the following: See the difference? 1 OR = See the difference? This time, we’ve created a new binary number that has 1’s whenever a 1 appears in a digit in either operand. Data: Binary Arithmetic

13 Data: Binary Arithmetic
Binary Logic (OR) We follow the same steps as before For each resulting digit, we write: 0 if neither of the digits is 1 1 if either/both of the digits is 1 Uses We can use bitwise OR to handle inputs on a controller. Imagine we have a box with two buttons – left and right. Pushing the left button returns a value of 1, and pushing the right button returns a value of 2. Both buttons return 0 of they aren’t pressed. Imagine these as two binary numbers: 01 and 10. See how they inhabit different bits? If we wanted to tell which buttons are being pressed in the game, we can bitwise OR them together to get a single number. We can then look through that number, bit-by-bit, to see what buttons we’re pressing. Num1 1 Num2 Result Data: Binary Arithmetic

14 Data: Binary Arithmetic
Perform a bitwise OR on the following numbers If the numbers are in binary, output the answer as binary If the numbers are in denary, output the answer as denary You will need to convert the numbers to binary to perform the bitwise AND ANSWERS From top-left to bottom-right: 1101 | 0101 0001 | 1010 0000 | 1111 12 | 2 1 | 3 5 | 10 Data: Binary Arithmetic

15 Data: Binary Arithmetic
Binary Logic (Shifts) The final form of ‘logic’ is shifts We can take any binary number and shift it to the left or right It makes the number look like it has moved Shifting left ‘adds’ a 0 least significant bit Shifting right ‘removes’ a 0 least significant bit, and adds a 0 most significant bit Original Number: 0110 Shifted Left (1): 1100 Shifted Right (1): 0011 Data: Binary Arithmetic

16 Data: Binary Arithmetic
Binary Logic (Shifts) There is one important thing to remember about shifts We can shift too much Any time we shift numbers ‘off’ the space we have, we’re replacing it with a 0 Original Number: 1111 Right Shift (1): 0111 Right Shift (3): 0001 Original Number: 1111 Left Shift (1): 1110 Left Shift (3): 1000 Original Number: 1111 Left Shift (2): 1100 Then Right Shift (1): 0110 By ‘shifting too much’, I mean completely throwing away any significant bits. In the bottom example, we shift left and then shift right. Because we shifted left too much, the most significant 1’s were thrown away (as they didn’t fit in the set-size binary numbers). When we shift right, we’ll notice that those 1’s have turned to 0’s. Data: Binary Arithmetic

17 Data: Binary Arithmetic
Binary Logic (Shifts) The best use of shifts is when we multiply a binary number by a power of 2 We shift left the same number of times as zeroes in the power 21 (shift once) 22 (shift twice) 23 (shift three times) Examples x 102 (1310 x 22) x 1002 (310 x 410) = (2610) = (1210) All we did here is shift the number left once Here we shifted left twice Data: Binary Arithmetic

18 Data: Binary Arithmetic
Perform the following shifts and convert the result to denary For each one, use a binary number size of 4 digits Right Shift (2): 0011 Right Shift (1): 1001 Right Shift (3): 1110 Left Shift (1): 0110 Left Shift (3): 0111 Left Shift (2): 1101 ANSWERS From top-left to bottom-right: Data: Binary Arithmetic

19 Data: Binary Arithmetic
Complete the following exercises Each row in the table asks for something different Convert each answer to denary Add (Set Size of 6 Bits) Bitwise AND (&) 24 & 12 10 & 8 32 & 16 Bitwise OR (|) 24 | 12 10 | 8 32 | 16 Convert Units 2.1KiB to KB 0.5MB to MiB 500GB to TiB ANSWERS From top-left to bottom-right: 8 8 0 2.1504KB MiB TiB Data: Binary Arithmetic

20


Download ppt "Topic 3: Data Binary Arithmetic."

Similar presentations


Ads by Google