Part 3: Negative Binary Numbers This standard method of binary number representation doesn't account for negative numbers. We can't just throw a negative sign in front of a binary number though - everything in a computer must be represented by 1's and 0's. One way we can get around this is by using the very first digit of each binary number to represent the sign: 0 for positive and 1 for negative. This is called 1's complement representation. 00010110  ->  0  0010110  ->  positive 22 10010110  ->  1  0010110  ->  negative 22   Using 1's complement representation we can represent the numbers -127 through 127 in a single byte (note that we can only go half as high as an unsigned number because half of the possible representations for that byte are being used for the negative numbers). However, there is a flaw with 1's complement: the representation of the number 0. 00000000  ->  0  0000000  ->  zero 10000000  ->  1  0000000  ->  negative zero? Because of this, we usually prefer to use 2's complement representation, which cleverly avoids the double 0 issue while still using the first digit of the binary number to show the sign. Even in 2's complement representation, if the first digit is 0 the number is positive (or 0), and if the first digit if 1 the number is negative. You can convert a positive binary number to it's 2's complement negative by following these steps: Reverse each bit of the number (0's turn to 1's and 1's turn to 0's) Add 1 For example, lets say we want to represent -99 in binary: 99 in binary is 01100011 Flipping all of the bits gets us 10011100 Adding 1 gets us 10011101 Therefore 10011101 is -99 in 2's complement binary.   The process to convert a negative binary number to a positive one is the exact same: -99 in binary is 10011101 Flipping all of the bits gets us 01100010 Adding 1 gets us 01100011 01100011 is 99 in binary, just like we started with   So far we've only shown negative numbers using 8 digit binary numbers. Like I mentioned above, we usually use this size because that represents a byte in the computer's memory. We could just as easily write our 2's complement binary numbers using any number of digits, and in that case the leftmost digit will always represent the sign of the number.   Exercise 6: How would you represent the number -5 in 2's complement binary? Show your answer as an 8 digit number.   Another convenient aspect of 2's complement binary representation is that it remains consistent with addition, which means we can add positive and negative binary numbers and get the mathematically correct result. This also allows us to easily perform subtraction: to subtract, reverse the sign of the subtracted number by doing the 2's complement conversion, then add that to your other number. Exercise 7: What is 01010010 - 00001110? Show the result in binary. (You can check your work by converting all of the numbers to decimal and verifying that the result is correct)

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter8: Arrays And Strings
Section: Chapter Questions
Problem 21PE
icon
Related questions
icon
Concept explainers
Question

Part 3: Negative Binary Numbers

This standard method of binary number representation doesn't account for negative numbers. We can't just throw a negative sign in front of a binary number though - everything in a computer must be represented by 1's and 0's. One way we can get around this is by using the very first digit of each binary number to represent the sign: 0 for positive and 1 for negative. This is called 1's complement representation.

00010110  ->  0  0010110  ->  positive 22

10010110  ->  1  0010110  ->  negative 22

 

Using 1's complement representation we can represent the numbers -127 through 127 in a single byte (note that we can only go half as high as an unsigned number because half of the possible representations for that byte are being used for the negative numbers). However, there is a flaw with 1's complement: the representation of the number 0.

00000000  ->  0  0000000  ->  zero

10000000  ->  1  0000000  ->  negative zero?

Because of this, we usually prefer to use 2's complement representation, which cleverly avoids the double 0 issue while still using the first digit of the binary number to show the sign. Even in 2's complement representation, if the first digit is 0 the number is positive (or 0), and if the first digit if 1 the number is negative. You can convert a positive binary number to it's 2's complement negative by following these steps:

  1. Reverse each bit of the number (0's turn to 1's and 1's turn to 0's)
  2. Add 1

For example, lets say we want to represent -99 in binary:

99 in binary is 01100011

Flipping all of the bits gets us 10011100

Adding 1 gets us 10011101

Therefore 10011101 is -99 in 2's complement binary.

 

The process to convert a negative binary number to a positive one is the exact same:

-99 in binary is 10011101

Flipping all of the bits gets us 01100010

Adding 1 gets us 01100011

01100011 is 99 in binary, just like we started with

 

So far we've only shown negative numbers using 8 digit binary numbers. Like I mentioned above, we usually use this size because that represents a byte in the computer's memory. We could just as easily write our 2's complement binary numbers using any number of digits, and in that case the leftmost digit will always represent the sign of the number.

 

Exercise 6: How would you represent the number -5 in 2's complement binary? Show your answer as an 8 digit number.

 

Another convenient aspect of 2's complement binary representation is that it remains consistent with addition, which means we can add positive and negative binary numbers and get the mathematically correct result. This also allows us to easily perform subtraction: to subtract, reverse the sign of the subtracted number by doing the 2's complement conversion, then add that to your other number.

Exercise 7: What is 01010010 - 00001110? Show the result in binary. (You can check your work by converting all of the numbers to decimal and verifying that the result is correct)

 

 

Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Control Structure
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning