Introduction: Manipulating Binary Data

In our previous lessons, we learned how computers represent negative numbers using Two's Complement and how to build circuits that add bits together. However, in a real computer, data is constantly moving between different parts of the system. Sometimes, you need to move a 4-bit number into an 8-bit slot, and other times, you need to perform subtraction using the same hardware that handles addition.

In this lesson, we will explore the "tricks" that make Two's Complement so powerful. You will learn how to change the size of a binary number without changing its value and how to turn a subtraction problem into a simple addition problem. These techniques allow us to keep our circuit designs small and efficient.

Sign Extension: Growing a Number
Truncation: Shrinking a Number
Addition In Two's Complement
Subtraction: The "Flip And Add 1" Trick
Summary And Practice Prep

In this lesson, we learned how to manage binary data more effectively:

  • Sign Extension: To make a number larger, copy the leftmost bit into the new spaces.
  • Truncation: To make a number smaller, remove bits from the left, but be careful not to change the value.
  • Addition: Two's Complement addition works just like normal addition, as long as you discard the final carry-out bit.
  • Subtraction: To subtract, flip the bits of the second number, add 1, and then add it to the first number.

Now, it is time to put these tricks to work! In the following practice exercises, you will go into the CodeSignal IDE to build circuits that resize numbers and perform subtraction. You will see firsthand how these simple rules allow a computer to handle complex math with ease.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal