Mixed Width Inputs
Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
So far, we have worked on the assumption that for any operation (such as &, +), both operands have to be the same number of bits. However, in this section we will discuss what happens when the operands are of different sizes.
If two operands are of different sizes (number of bits), before we can do an operation like addition, we need to extend the shorter operand to match the length of the longer one. Depending on the type of the operands, how we extend the numbers differs. Our goal is to maintain the same value for the number after increasing the number of bits used to represent it. This means that if we are extending an unsigned number, we will always append leading zeroes to the number. However, if we are extending a signed number, then we want to preserve the magnitude and the sign of our number. If the MSB is 0, we append leading 0's; if it is 1, we append leading 1's. Two's complement representation has the nice property that if you append leading 1's to a negative number, the value will be preserved.
For the following questions, you will be given some pseudocode and will be asked to compute the result (using either signed or unsigned operands) in binary.
Sometimes when we add two positive numbers or two negative numbers, the bitwidth may be insufficient to properly represent our result. When this occurs, we say that our operation overflowed. To detect overflow, you can always sanity check your operation. If you added two positive numbers and ended up with a negative, or if you added two negative numbers and ended up with a positive, then overflow occurred. To avoid this overflow, you must first extend the width of your input operands by 1-bit and then perform the computation.
unsigned a = 0b01001;
unsigned b = 0b011;
result = a + b;
result = 0b?
signed a = 0b1010;
signed b = 0b00011;
result = a + b;
result = 0b?
unsigned a = 0b10010;
unsigned b = 0b011011;
result = a + b;
result = 0b?
signed a = 0b11011;
signed b = 0b1001;
result = a - b;
result = 0b?
signed a = 0b01011;
signed b = 0b0110;
result = a + b;
result = 0b?
Comparisons of different sizes
Like arithmetic, we can also do comparisons of different sizes. We'll see a few examples in the following questions.
unsigned a = 0b1100;
unsigned b = 0b100;
result = a == b;
result = ?
signed a = 0b1100;
signed b = 0b100;
result = a == b;
result = ?
signed a = 0b0110;
signed b = 0b11010;
result = a > b;
result = ?