Topic 3 Data Binary Arithmetic Computer Science Binary
Topic 3: Data Binary Arithmetic Computer Science
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 2 Data: Binary Arithmetic
Binary Arithmetic (Addition) • Here’s an example 0 1 1 + 0 0 1 1 0 = 1 0 0 0 1 Carries 0 1 1 1 0 • Here we’re adding 01011 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 3 Essentially, long addition! Data: Binary Arithmetic
Binary Arithmetic (Addition) • We can always check our work afterwards Convert binary numbers to denary • Including our answer 0 1 1 + 0 0 1 1 0 = 1 0 0 0 1 Carries 0 1 1 1 0 010112 → 1110 001102 → 610 11 + 6 = 17 Add up denary numbers • See if the two answers match 100012 → 1710 Same answer, therefore we are correct! 4 Data: Binary Arithmetic
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 + = Carrie s 5 1? 1 1 0 0 0 1 1 1 0 Data: Binary Arithmetic
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 + = Carrie s 6 1? 1 1 0 0 0 1 1 1 0 Data: Binary Arithmetic Set Size No Answer: 00001 Answer: 100001 In Denary: 33
• 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 7 No Set Size 0011 + 1001 1010 + 0001 1111 + 0001 Set Size (4 bits) 1010 + 1010 0000 + 0111 1111 + 1111 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 -AND | | -- OR Rather than booleans • These operations are called bitwise AND, and bitwise OR bitwise AND: bitwise OR: 8 & | Data: Binary Arithmetic
Binary Logic (AND) • Let’s say we have two binary numbers 0101 1100 • Performing an AND on them gives us the following: 0 1 AND 1 1 0 0 = 0 1 0 0 • Can you tell what’s happened? 9 Data: Binary Arithmetic
Binary Logic (AND) • We go through both numbers Digit by digit Num 1 0 0 1 1 Num 2 Resul t 0 1 0 0 0 1 • For each resulting digit, we write: 10 0 if either of the digits is 0 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: 0011110010110100 Subnet Mask: 000011110000 AND Result: 000010110000 See how the only 1’s left are the ones also in the subnet mask? 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 11 1101 & 0101 0001 & 1010 0000 & 1111 12 & 2 1 & 3 5 & 10 Data: Binary Arithmetic
Binary Logic (OR) • Let’s take the same numbers from before 0101 1100 • Performing an OR on them gives us the following: 0 1 OR 1 1 0 0 = 1 1 0 1 • See the difference? 12 Data: Binary Arithmetic
Binary Logic (OR) • We follow the same steps as before Num 1 0 0 1 1 Num 2 0 1 Resul t 0 1 1 1 • For each resulting digit, we write: 13 0 if digits 1 if digits 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 neither of the pressed in the game, we can bitwise OR them together to get a single number. is 1 We can then look through that number, bit-by either/both of the Data: Binary Arithmetic -bit, to see what buttons we’re pressing. is 1
• 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 14 1101 | 0101 0001 | 1010 0000 | 1111 12 | 2 1 | 3 5 | 10 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 Original Number: 0110 Shifted Left (1): 1100 Shifted Right (1): 0011 • 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 15 Data: Binary Arithmetic
Binary Logic (Shifts) • There is one important thing to remember about shifts We can shift too much Original Number: 1111 Right Shift (1): Left Shift (1): 0111 1110 Original Number: Right Shift (3): Left Shift (3): 1111 0001 1000 Left Shift (2): 1100 Then Right Shift 0110 time we shift(1): numbers ‘off’ the space • Any have, we’re replacing it with a 0 16 Data: Binary Arithmetic we
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 17 21 (shift once) times) 22 (shift twice) 23 (shift three Data: Binary Arithmetic
• Perform the following shifts and convert the result to denary For each one, use a binary number size of 4 digits 18 Right Shift (2): 0011 Right Shift (1): 1001 Right Shift (3): 1110 Left Shift (1): 0110 Left Shift (3): 0111 Left Shift (2): 1101 Data: Binary Arithmetic
• Complete the following exercises Each row in the table asks for something different Convert each answer to denary 19 Add (Set Size of 6 Bits) 010100 + 010110 101010 + 0001111 + 110011 Bitwise AND (&) 24 & 12 10 & 8 32 & 16 Bitwise OR (|) 24 | 12 10 | 8 32 | 16 Convert Units 2. 1 Ki. B to KB 0. 5 MB to Mi. B 500 GB to Ti. B Data: Binary Arithmetic
- Slides: 20