Answered step by step
Verified Expert Solution
Question
1 Approved Answer
USE Python: 2. Two's Complement (Signed Numbers in Binary) (6 points) The version of binary representation that we discussed last week is unsigned, meaning that
USE Python:
2. Two's Complement (Signed Numbers in Binary) (6 points) The version of binary representation that we discussed last week is unsigned, meaning that it only (correctly) handles positive integers (and 0); there's no way to represent, say, -5 (short of sticking a character in front of the binary for 5). In the real world, negative numbers are a part of life, so we need a way to deal with signed (positive and/or negative values To get around this limitation, modern computers use a variation of binary called two's complement representation. Two's complement allows us to represent both positive and negative numbers in binary, with the leftmost bit serving as the sign bit (if the sign bit is 0, then the number is positive (or 0); if the sign bit is 1, then the number is negative). Two's complement isn't as simple as simply sticking a sign bit at the beginning of a binary value though; the presence of the sign bit modifies many of the other bits in the number (meaning that the binary representations for +5 and -5 look very different from one another). For example, the 4-bit-long representation of +5 in two's complement is 0101, while the 4-bit- long two's complement representation of -5 is 1011 Two's complement values are also fixed in length at the time that they are created, the same negative number will have (slightly) different two's complement representations based on the number of bits that were used to create it. For example, the 4-bit-long two's complement representation of -5 is 1011, while the 8-bit-long two's complement representation of -5 is 11111011. Fortunately, if you look closely, there's a trick to this: in order to get the longer (more bit) version of the number, we simply extended (copied) the sign bit until we reached the desired length Python provides a built-in function named bin() that converts base 10 (decimal) values into binary, but itonly works correctly with positive integers, and it adds "0b" to the beginning of the number's string representation. Neither of these is acceptable for us For this part of the lab, we will develop a Twoscomplement class that will store positive and negative base 10 integers in two's complement binary form. Our class will also allow the user to translate two's complement numbers back to base 10 2. Two's Complement (Signed Numbers in Binary) (6 points) The version of binary representation that we discussed last week is unsigned, meaning that it only (correctly) handles positive integers (and 0); there's no way to represent, say, -5 (short of sticking a character in front of the binary for 5). In the real world, negative numbers are a part of life, so we need a way to deal with signed (positive and/or negative values To get around this limitation, modern computers use a variation of binary called two's complement representation. Two's complement allows us to represent both positive and negative numbers in binary, with the leftmost bit serving as the sign bit (if the sign bit is 0, then the number is positive (or 0); if the sign bit is 1, then the number is negative). Two's complement isn't as simple as simply sticking a sign bit at the beginning of a binary value though; the presence of the sign bit modifies many of the other bits in the number (meaning that the binary representations for +5 and -5 look very different from one another). For example, the 4-bit-long representation of +5 in two's complement is 0101, while the 4-bit- long two's complement representation of -5 is 1011 Two's complement values are also fixed in length at the time that they are created, the same negative number will have (slightly) different two's complement representations based on the number of bits that were used to create it. For example, the 4-bit-long two's complement representation of -5 is 1011, while the 8-bit-long two's complement representation of -5 is 11111011. Fortunately, if you look closely, there's a trick to this: in order to get the longer (more bit) version of the number, we simply extended (copied) the sign bit until we reached the desired length Python provides a built-in function named bin() that converts base 10 (decimal) values into binary, but itonly works correctly with positive integers, and it adds "0b" to the beginning of the number's string representation. Neither of these is acceptable for us For this part of the lab, we will develop a Twoscomplement class that will store positive and negative base 10 integers in two's complement binary form. Our class will also allow the user to translate two's complement numbers back to base 10Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started