I\'m stuck on this whole thing. He threw this on homework but has never explaine
ID: 3627284 • Letter: I
Question
I'm stuck on this whole thing. He threw this on homework but has never explained anything in binary beyond converting to decimal and hex. Please explain how to do this problem. Thanks! Here's the problem:Assume a binary number x is of the form
x = x4*x3*x2*x1*x0
That is, x is represented by a binary string of length 5, where x0 ... x4 are the bits of this representation.
Let y be the number of the form
y = 1x = 1*x4*x3*x2*x1*x0
That is, y is obtained from x by adding to its binary representation a 1 on the left.
What is the value of the difference y - x (in decimal)? Explain.
Hint: try several examples. Take a 5-bit binary number x, form the corresponding y, convert x and y into decimal, and compute the difference. Observe the pattern. Provide an explanation.
Explanation / Answer
"<<" being the leftward bit-shift operator:
http://en.wikipedia.org/wiki/Bitwise_operation#Shifts_in_C.2C_C.2B.2B.2C_C.23
each of the following {x0, x1, x2, x3, x4} being a binary digit of zero or one. in this case the subscript of each x-digit corresponds to the power of two, thus
xN << N = xN * (2N)
x = (x4 << 4) + (x3 << 3) + (x2 << 2) + (x1 << 1) + (x0 << 0)
x = (x4 * 16) + (x3 * 8) + (x2 * 4) + (x1 * 2) + (x0 * 1)
y = (1 << 5) + ( (x4 << 4) + (x3 << 3) + (x2 << 2) + (x1 << 1) + (x0 << 0) )
y = (1 * 32) + ( (x4 * 16) + (x3 * 8) + (x2 * 4) + (x1 * 2) + (x0 * 1) )
noting that the latter part of y is equal to x:
y = 32 + ( x )
y - x = 32 (solution)
examples may include:
x = 01010 (binary) = 10
y = 101010 (binary) = 42