TutorChase logo
Login
OCR GCSE Computer Science Notes

2.4.2 Binary Arithmetic and Shifts

Binary arithmetic and shifts are fundamental skills in computer science, helping computers process, store, and manipulate data efficiently at the binary level.

Adding Two Binary Integers

Binary addition follows a set of very simple rules, much like decimal addition but only using the digits 0 and 1.

Binary Addition Rules

  • 0 + 0 = 0

  • 0 + 1 = 1

  • 1 + 0 = 1

  • 1 + 1 = 0 (carry 1 to the next higher bit)

When adding two binary numbers, it is essential to start from the least significant bit (LSB), which is the bit farthest to the right.

Example: Binary Addition

Add 1101 and 1011:

markdown

  1101
+  1011
-------
  11000

Step-by-step:

  1. 1 + 1 = 0 (carry 1)

  2. 0 + 1 + 1 (carry) = 0 (carry 1)

  3. 1 + 0 + 1 (carry) = 0 (carry 1)

  4. 1 + 1 (carry) = 0 (carry 1)

  5. Add the final carry: 1

Result: 11000

Carrying in Binary Addition

Just like in decimal addition, when the sum exceeds the largest single-digit binary number (1), you carry over to the next bit to the left.

Overflow Errors

An overflow error occurs when the result of an addition exceeds the maximum value that can be represented with the given number of bits.

How Overflow Happens

  • In an 8-bit system, the largest number is 255 (binary 11111111).

  • Adding two numbers whose result is greater than 255 causes an overflow.

Example of Overflow

Adding 11111111 and 00000001:

markdown

CopyEdit

11111111
+ 00000001
-----------
 100000000

Here, the result is 9 bits, but only 8 bits are allowed. The extra bit is lost, causing an overflow error.

Importance of Handling Overflow

Overflow can cause programs to behave unpredictably or produce incorrect results, so it's vital that students recognize and handle such errors.

Most Significant Bit (MSB) and Least Significant Bit (LSB)

Least Significant Bit (LSB)

  • The LSB is the rightmost bit in a binary number.

  • It represents the smallest value (1).

  • Example: In 1011, the LSB is the last 1.

Most Significant Bit (MSB)

  • The MSB is the leftmost bit in a binary number.

  • It represents the largest value.

  • Example: In 1011, the MSB is the first 1.

Importance of MSB and LSB

  • MSB indicates the magnitude and sometimes the sign (in certain number systems).

  • LSB changes first when a binary number increments.

Understanding the MSB and LSB is crucial for operations like binary shifts, where bits are moved left or right.

Binary Shifts

Binary shifts move all bits in a binary number to the left or right, filling the gap with 0.

There are two main types of binary shifts:

  • Left shift

  • Right shift

Left Shifts

In a left shift, each bit moves one place to the left.

  • A 0 is added to the right.

  • The MSB is discarded if it shifts past the 8-bit boundary.

Effect: Multiplying the number by 2 for each place shifted.

Example: Left Shift by 1

Original: 00010110 (22)

After left shift by 1:

yaml

CopyEdit


Original: 00010110
Shifted : 00101100

New value: 44 (22 × 2)

Example: Left Shift by 2

Shift by 2 places:

yaml

CopyEdit

Original: 00010110
Shifted : 01011000

New value: 88 (22 × 4)

Right Shifts

In a right shift, each bit moves one place to the right.

  • A 0 is added to the left.

  • The LSB is discarded.

Effect: Dividing the number by 2 for each place shifted (ignoring any remainders).

Example: Right Shift by 1

Original: 00010110 (22)

After right shift by 1:

yaml

CopyEdit

Original: 00010110
Shifted : 00001011

New value: 11 (22 ÷ 2)

Example: Right Shift by 2

Shift by 2 places:

yaml

CopyEdit

Original: 00010110
Shifted : 00000101

New value: 5 (floor division of 22 ÷ 4)

Summary of Shift Effects

  • Left shift by 1 → multiply by 2

  • Left shift by 2 → multiply by 4

  • Right shift by 1 → divide by 2 (floor division)

  • Right shift by 2 → divide by 4 (floor division)

Shifting is a fast method used by computers for multiplication and division by powers of two.

Practical Applications of Binary Shifts

Left Shifts in Multiplication

When computers multiply numbers, they often use left shifts instead of traditional multiplication to speed up calculations.

Example:

  • 3 × 8

  • 3 in binary = 00000011

  • Left shift by 3 positions:

00000011 → 00011000

New binary value: 24 (3 × 2³ = 24)

Right Shifts in Division

Similarly, right shifts are used to divide numbers quickly by powers of two.

Example:

  • 64 ÷ 4

  • 64 in binary = 01000000

  • Right shift by 2 positions:

01000000 → 00010000

New binary value: 16

Data Compression and Encryption

Binary shifts are commonly used in:

  • Data compression to rearrange bits and optimize space.

  • Cryptography to encode or scramble data securely.

Both fields rely on precise, predictable changes to bit patterns, which binary shifts can efficiently achieve.

Overflow in Binary Shifts

Shifting can also lead to overflow, particularly with left shifts.

Example of Overflow

Original 8-bit binary: 11111111 (255)

Left shift by 1:

11111111 → 11111110 (after discarding MSB)

Expected result if not constrained: 510 (but limited by 8 bits)

This overflow can cause significant errors if not properly handled in programming and digital systems.

Important Tips for Binary Arithmetic and Shifts

  • Always check the number of bits you are allowed to use (often 8 bits for GCSE).

  • Be cautious about overflow when adding or left shifting.

  • Remember that shifting right loses information (bits dropped are gone permanently).

  • Understand the role of MSB and LSB to anticipate how shifts and additions affect the binary number.

  • Practice bnary addition and shifts often to develop fluency.

Key Termsi to Remember

  • Binary Addition: Adding two binary numbers using carry rules.

  • Overflow Error: When the result exceeds the available number of bits.

  • Most Significant Bit (MSB): Leftmost bit, holds the highest value.

  • Least Significant Bit (LSB): Rightmost bit, holds the smallest value.

  • Left Shift: Moves bits to the left, multiplies the value by powers of two.

Right Shift: Moves bits to the right, divides the value by powers of two.

FAQ

If you left shift a binary number and the most significant bit (MSB) is a 1, the bit will be pushed out of the 8-bit boundary during the shift, resulting in data loss and a potential overflow. Once the MSB is shifted out, it is discarded, and a 0 is placed at the least significant bit (LSB) on the right. This can dramatically change the value of the binary number because the highest place value (128 in 8-bit binary) is lost. For example, if you left shift 10000001 (129 in decimal) by one position, you get 00000010, which is 2 in decimal — a drastic change. This is why it is critical to check the MSB before performing a shift, especially in systems where overflow errors can cause incorrect program behavior or system crashes. Handling overflow typically involves validation before shifting or using larger bit-width storage.

A logical shift and an arithmetic shift both move bits left or right, but they handle the vacant positions differently, especially during right shifts. In a logical shift, whether shifting left or right, empty positions are always filled with zeros. Logical shifts are usually used for binary numbers that represent unsigned data. In contrast, an arithmetic shift preserves the sign of a signed binary number (using two’s complement). In an arithmetic right shift, the leftmost bit (sign bit) is copied into the new position instead of adding a zero. This preserves whether the number is positive or negative. For example, an arithmetic right shift of 11110000 keeps the MSB 1 intact, resulting in 11111000, rather than 01111000, which would happen in a logical shift. Arithmetic shifts are crucial when dealing with signed binary numbers, where maintaining the number’s sign is important for correct calculations.

Right shifting divides a binary number by two because each rightward movement of the bits halves the value represented by each position. In binary, the place values are powers of two (128, 64, 32, 16, etc.). Moving all bits one place to the right effectively shifts each bit into a position worth half as much. However, since binary numbers must be whole numbers (integers), the fractional part (the remainder) is discarded. This is called floor division. For example, if you right shift the binary for 7 (00000111) once, you get 00000011, which is 3 in decimal, not 3.5. The 0.5 is ignored because standard binary storage doesn’t represent non-whole numbers unless using floating-point formats, which are beyond simple binary shifts. Understanding this helps students avoid mistakes when shifting numbers that do not divide evenly by two.

Multiple consecutive binary shifts amplify the effect of the original operation exponentially. For left shifts, each shift multiplies the number by two. Therefore, two consecutive left shifts multiply the original number by four (2²), three consecutive shifts multiply by eight (2³), and so on. Similarly, consecutive right shifts divide the number by increasing powers of two, discarding any remainders. For example, if you left shift 00000011 (3 in decimal) two times, you first get 00000110 (6) and then 00001100 (12). If you right shift 00001100 (12) two times, you first get 00000110 (6) and then 00000011 (3). Each shift operation compounds the effect. However, with each shift, there is a risk of overflow for left shifts or total data loss for right shifts if all significant bits are shifted out. Thus, careful planning is needed when performing multiple shifts.

You can predict a binary addition overflow by analyzing the most significant bits (MSBs) of the numbers being added. If both numbers have a 1 in the MSB and you are working within a fixed bit limit (like 8 bits), the addition will definitely cause an overflow. This is because adding two large numbers where each already uses the highest available bit position exceeds the range that can be stored. Another clue is when the sum of the numbers exceeds the maximum value for the bit limit — for 8 bits, anything greater than 255. In some cases, even if the MSBs are different, carry propagation from lower bits can still cause an overflow, but MSB analysis is a quick way to predict issues. Experienced programmers often build in pre-checks before carrying out binary addition operations to detect and prevent overflows that would otherwise cause errors or unpredictable results.

Practice Questions

Explain what happens when a binary number is shifted two places to the left. Give an example using the 8-bit binary number 00001101.

When a binary number is shifted two places to the left, each bit moves two places to the left and two zeros are added on the right. This effectively multiplies the original number by four. For the binary number 00001101 (which is 13 in decimal), shifting left by two places results in 00110100. In decimal, 00110100 equals 52, and 13 × 4 = 52. However, if the shift causes bits to move past the 8-bit boundary, those bits are lost, potentially leading to an overflow error if not handled properly.

Describe what is meant by an overflow error in binary addition and explain how it occurs with 8-bit numbers.

An overflow error in binary addition happens when the result of adding two binary numbers is too large to fit within the designated number of bits, such as 8 bits. For example, adding 11111111 (255 in decimal) and 00000001 (1 in decimal) results in 100000000, which is 9 bits. Since only 8 bits are allowed, the leftmost bit is lost, and the result wraps around incorrectly to 00000000. Overflow errors are critical because they can cause programs to produce incorrect outputs or crash if proper error checking is not in place.

Hire a tutor

Please fill out the form and we'll find a tutor for you.

1/2
Your details
Alternatively contact us via
WhatsApp, Phone Call, or Email