OCR A-Level Computer Science H446
Processors: Structure, Performance and Types
Input, Output and Storage
Systems Software
Applications Generation
Software Development
Types of Programming Language
Exchanging Data
Networks
Web Technologies
Data Types and Number Representation
Data Structures
Boolean Algebra
Legal, Moral, Cultural and Ethical Issues
Elements of Computational Thinking
Problem Solving and Programming
Algorithms
Data Types and Number Representation
Bitwise shifts
- 3 things to know
- Component 01
Overview
Recall questions on logical left and right shifts and their effect on value, bits lost off the end, arithmetic right shifts on two's complement numbers, rounding, compilers replacing multiplication with shifts, and extracting packed values.
What you need to know
Answer each one before you reveal it.
Shift 01001100 two places right logically and check the result.
What is an arithmetic right shift, and how does it differ from a logical one?
Questions from this subtopic
Each one with the model answer underneath.
- 1
What does a logical shift do to a binary pattern?
Model answer
It moves every bit a given number of places left or right. Bits pushed off the end are lost, and the positions vacated at the other end are filled with zeros. A logical shift makes no assumption about the pattern meaning a number, and in particular it treats the leftmost bit as an ordinary bit rather than a sign.
- 2
What effect does a logical left shift of n places have on the value, and why?
Model answer
It multiplies the value by 2 to the power n. Every bit moves to a place worth twice as much, and doing that n times multiplies by 2 n times. It is the binary equivalent of adding zeros on the right of a denary number to multiply by ten — but the word is a fixed width, so once a significant bit falls off the left-hand end the result has overflowed and is no longer the value multiplied.
- 3
Shift 00010110 two places left. What is the result in binary and in denary?
Model answer
01011000, which is 88. The original is 22, and shifting two places left multiplies by 4, giving 88. Note the two zeros shifted in at the right-hand end.
- 4
Shift the unsigned byte 10110000 one place left. What goes wrong?
Model answer
The result is 01100000, which is 96. The original value was 176, and doubling it should give 352, but 352 will not fit in eight bits. The leading 1 was pushed off the left-hand end and lost, taking 256 with it, and 352 minus 256 is indeed 96. This is where the shift-as-multiplication shortcut stops working: it is only valid while no significant bit falls off the top.
- 5
What effect does a logical right shift have, and what is lost?
Model answer
It divides an unsigned value by 2 to the power n, and what is lost is the remainder. The bits pushed off the right-hand end are the fractional part of the answer and they are simply discarded, so a right shift performs integer division that always rounds down. Shifting 00001011, which is 11, two places right gives 00000010, which is 2, not 2.75.
- 6
Shift 01001100 two places right logically and check the result.
Model answer
00010011, which is 19. The original is 76, and 76 divided by 4 is exactly 19, so nothing was lost this time because the two bits shifted off the end were both zero.
Other ways to work this
The same content, four other ways.
What is inside Levo
Six ways to work the same material.
Every question in Data Types and Number Representation, on a schedule.
Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.
Or £46 a year, about £3.83 a month. Cancel anytime.