Computer Science AS CIE
-
data-representation as5 主题
-
multimedia as3 主题
-
compression as2 主题
-
networks-and-the-internet as11 主题
-
computers-and-components as5 主题
-
logic-gates-and-logic-circuits as2 主题
-
central-processing-unit-cpu-architecture as6 主题
-
assembly-language- as4 主题
-
bit-manipulation as1 主题
-
operating-systems as3 主题
-
language-translators as2 主题
-
data-security as3 主题
-
data-integrity as1 主题
-
ethics-and-ownership as3 主题
-
database-concepts as3 主题
-
database-management-systems-dbms- as1 主题
-
data-definition-language-ddl-and-data-manipulation-language-dml as1 主题
-
computational-thinking-skills as1 主题
-
algorithms as4 主题
-
data-types-and-records as2 主题
-
arrays as2 主题
-
files as1 主题
-
introduction-to-abstract-data-types-adt as1 主题
-
programming-basics as1 主题
-
constructs as2 主题
-
structured-programming as1 主题
-
program-development-life-cycle as1 主题
-
program-design- as2 主题
-
program-testing-and-maintenance as3 主题
bitwise-operations as
Exam code:9618
Binary shifts
What are binary shifts?
-
A binary shift is an operation that moves all the bits in a binary number left or right by a certain number of positions
-
Often used in programming and computer systems for fast multiplication or division by powers of 2, or for manipulating individual bits
-
There are three main types of binary shift:
-
Logical
-
Arithmetic
-
Cyclic
-
Logical
-
Moves bits left or right and fills the gap with 0s
-
Used for unsigned binary numbers or raw bit manipulation
|
Direction |
What happens |
|---|---|
|
Left |
All bits shift left, a |
|
Right |
All bits shift right, a |
Left
-
The following number is shifted by two places to the left
|
Step |
128 |
64 |
32 |
16 |
8 |
4 |
2 |
1 |
|---|---|---|---|---|---|---|---|---|
|
Original |
0 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
|
Left shift by 2 |
0 |
0 |
1 |
1 |
1 |
0 |
0 |
0 |
-
Original number: 0000 1110 = 14
-
Left shift (2) result: 0011 1000 = 56
-
Each left shift has doubled the number:
-
Original value = 14
-
Left shift 1 – Doubled the number to 28
-
Left shift 2 – Doubled the number to 56
-
Right
-
The following number is shifted by three places to the right
|
Step |
128 |
64 |
32 |
16 |
8 |
4 |
2 |
1 |
|---|---|---|---|---|---|---|---|---|
|
Original |
1 |
1 |
0 |
0 |
1 |
0 |
0 |
0 |
|
Right shift by 3 |
0 |
0 |
0 |
1 |
1 |
0 |
0 |
1 |
-
Original number: 1100 1000 = 200
-
Right shift (3) result: 0001 1001 = 25
-
Each right shift has halved the number:
-
Original value = 200
-
Right shift 1 – Halved the number to 100
-
Right shift 2 – Halved the number to 50
-
Right shift 3 – Halved the number to 25
-
Arithmetic
-
Moves bits, but preserves the sign bit (used for signed binary numbers in two’s complement)
|
Direction |
What happens |
|---|---|
|
Left |
Same as logical left (shift left, 0 in) |
|
Right |
Shift right, copy the sign bit (MSB) into the new leftmost bit |
Right
-
The following number is shifted by three places to the right
|
Step |
128 |
64 |
32 |
16 |
8 |
4 |
2 |
1 |
|---|---|---|---|---|---|---|---|---|
|
Original |
1 |
1 |
1 |
0 |
1 |
0 |
0 |
0 |
|
Right shift by 3 |
1 |
1 |
1 |
1 |
1 |
0 |
1 |
0 |
-
Original number: 1110 1000
= −24 -
Right shift (3) result: 1111 1010 = −6
-
Each arithmetic right shift divides the number by 2, rounding towards negative infinity (preserving the sign bit)
-
Original value = −24
-
Right shift 1 → 1111 0100 = −12
-
Right shift 2 → 1111 1010 = −6
-
Right shift 3 → 1111 1101 = −3
-
Cyclic
-
Rotates the bits around, nothing is lost, no 0s added
-
The bit that falls off one end is reused at the other end
|
Direction |
What happens |
|---|---|
|
Left |
Leftmost bit moves to the rightmost position |
|
Right |
Rightmost bit moves to the leftmost position |
-
The following number is shifted by three places to the left and right
Left and Right
|
Step |
128 |
64 |
32 |
16 |
8 |
4 |
2 |
1 |
|---|---|---|---|---|---|---|---|---|
|
Original |
1 |
0 |
1 |
1 |
0 |
0 |
0 |
1 |
|
Cyclic left (×3) |
1 |
0 |
0 |
0 |
1 |
1 |
0 |
1 |
|
Cyclic right (×3) |
0 |
0 |
1 |
1 |
0 |
0 |
1 |
0 |
-
The 3 leftmost bits 101 wrap around to the right side
-
The 3 rightmost bits 001 wrap around to the left side
-
Original: 1011 0001 = 177
-
Cyclic left (3): 1000 1101 = 141
-
Cyclic right (3): 0011 0010 = 50
|
Shift type |
Left shift |
Right shift |
Used for |
|---|---|---|---|
|
Logical Shift |
Shift bits left, fill with |
Shift bits right, fill with |
Unsigned numbers, raw bits |
|
Arithmetic Shift |
Same as logical left |
Shift bits right, preserve sign bit |
Signed numbers (two’s complement) |
|
Cyclic Shift |
Rotate bits left (no loss) |
Rotate bits right (no loss) |
Bit rotation, encryption, checksums |
Device control & bit masking
What is device control?
-
In computer systems and embedded devices, each bit in a byte can be used to represent the state of a device or feature (e.g. turning a light on, checking if a sensor is active, or flagging an error)
-
You can control or monitor these bits using bitwise operations and bit masking
What is bit masking?
-
Bit masking uses binary patterns (masks) to test, set, clear, or toggle specific bits without affecting the others
Bitwise AND operation (&)
-
Used to test if a specific bit is set to 1
-
Only returns 1 when both the binary value and the mask have 1 in the same position
-
Useful for checking the status of a device
|
Description |
128 |
|---|
Responses