Download presentation
Presentation is loading. Please wait.
1
A Level Computing Component 2
3M, N, O
2
Assessment Outcomes 3M (A2) Explain and use shift functions: logical and arithmetic shifts. 3N (A2) Interpret and apply shifts in algorithms and programs. 3O (A2) Describe the causes of overflow and underflow.
3
Shift Functions 1 Shift Left 3 Shift Right 0000 1111 = 15
A shift function moves bits to the left or the right. If the bits are shifted to the left, the left-most bit drops away and a zero is added to the right-most bit. If the bits are shifted to the right, then the right-most bit drops away and a zero is added to the left-most bit. 1 Shift Left 3 Shift Right = 15 A Left shift of 1 would move everything 1 to the left: = 30 = 15 A Right shift of 3 moves everything to the right = 1
4
Uses of Shift Functions
Run the following code in Python: x = 15 print ('x is', x) print ('x shifted one place to the left is', x << 1) print ('x shifted two places to the left is', x << 2) print ('x shifted three places to the left is', x << 3) print ('x shifted four places to the left is', x << 4) x = 15 print ('x is', x) print ('x shifted one place to the left is', x >> 1) print ('x shifted two places to the left is', x >> 2) print ('x shifted three places to the left is', x >> 3) print ('x shifted four places to the left is', x >> 4)
5
Use of shift functions Did you notice a pattern when you shifted bits to the left? You should have seen that the original number was multiplied by two with each bit shift to the left. Moving the bits to the right is the same as dividing by two, although you can see the problem with rounding that occurred..
6
Uses of Shift Functions – Logical Operations
AND OR 35 AND 2 written in binary is: AND print ('35 AND 2 is AND = ', 35 & 2) Where a bit is 1 in both binary numbers it will become a 1 in the output.. 38 OR 19 written in binary is: OR print ('38 OR 19 is OR = ', 38 | 19) Where a bit is 1 in One or the Other binary number it will become a 1 in the output..
7
Uses of Shift Functions – Logical Operations
XOR NOT 38 XOR 19 in binary is written XOR print ('38 XOR 19 is XOR = ', 38 ^ 19) You can find the complement of a number by using a NOT operation. For example the complement of 60: (using 2s complement)
8
3N Exercises Complete the 3N exercises to apply arithmetic shifts and logical operations.
9
3O - Interpret and apply shifts in algorithms and programs.
Overflow and underflow are general terms. They describe the situation when something becomes too big or too small to be processed correctly or stored in the space allocated to it correctly. If you have a fixed-size data structure such as a stack that has been set up to hold twenty data items, and you try to add a twenty first data item, this will cause a 'stack overflow' error. This is because the data structure is not big enough to hold the extra piece of data you are trying to add to it.
10
Overflows – 8 Bit Binary Imagine you are trying to add two 8-bit numbers in an 8-bit computer. The largest number that a register or memory location can hold is 8 bits. What happens when you add these two numbers? The answer is: This of course has 9 bits! As our memory locations can only hold 8 bits, that extra bit on the far left causes an 'overflow' error. This signals that the result was too big to hold in 8 bits. The sum we just did was , which equals 384. The largest number that 8 bits can hold is or 255 so having an overflow error makes sense with this calculation.
11
Overflows – Floating Point
Going back to decimal numbers for a moment, you can represent a number in scientific notation as follows The first part is called the 'mantissa'. The mantissa also holds the sign of the number. The second part is called the 'exponent' and it defines where the decimal point needs to be if the number is shown in its standard decimal form. In this case it indicates that this number is to be multiplied by a thousand and so the decimal point moves 3 locations to the right, like this Binary floating point uses the same idea. A binary floating point number is in two parts. The Mantissa and the Exponent. Both the mantissa and the exponent is in twos complement format. So if there is a 1 present in the leftmost bit of the mantissa, then this is a negative binary number. The exponent part is indicating where the decimal point needs to be.
12
Floating Point Let's break down this particular number and see what it represents. 1.001 mantissa. The most significant bit is a 1 and we know it is in two's complement so this must be a negative number. 0010 exponent. This indicates how many places the the decimal point needs to move. If it is a positive exponent, then you move the decimal point to the right. If it was a negative exponent, then you would slide the decimal point to the left. The exponent is 0010, so this is a positive 2. Slide the decimal point of the mantissa 2 positions to the right. So the full binary number is 1.001 becomes 100.1 It is in two complement and it is a negative number so flip the bits to the left of the last 1 011.1 The number is -3.5 decimal
13
Floating Point OverFlows
Because the highest exponent is 15 in 4 bits it can easily create an overflow. If there needed to be more than 15 shifts then there would be an exponent overflow.
14
Underflows The smallest positive number we can hold is when the mantissa is the smallest positive number and the exponent is the largest negative number. The smallest positive (normalised) mantissa is and the largest negative exponent is 100 (-4 as a 2s complement number). The smallest positive number we can hold is therefore Programs respond to underflow conditions in different ways. Some report an error, while others approximate as best they can and continue processing. For example, if your computer supports eight decimal places of precision and a calculation produces the number (with nine decimal places), an underflow condition occurs.
15
3O Exercises Explain the different types of Overflow using examples
Explain how an underflow occurs when a computer is trying to store a small value Explain how an underflow can occur when a computer is trying to store a number with too fine accuracy
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.