I want to understand the following code. What kinda shifting 3rd line is doing?
number = 1.265
bits = 8
shifted_no = 1.265 * (2** bits)
If I check the binary format of the results of number and shifted_no :
0011 1111 1010 0001 1110 1011 1000 0101
0100 0001 0010 0001 1110 1011 1000 0101
Thanks.
Here number
is a 32-bit floating point. A floating point has the structure:
+-+--------+-----------------------+
|s| exp | mantisse |
+-+--------+-----------------------+
1 8 23
This represents a value (-1)1-2×s×2e-127×1.m with s the value of s
, e the value of exp
and m
the value of mantisse
.
With the number of bits underneath each component. If you multiply a float with a power of two, the exponent part is incremented with that power. So since you multiply with eight (there is probably an error in your question), you increment the exponent with three and obtain.
Original floating point:
+-+--------+-----------------------+
|0|01111111|01000011110101110000101|
+-+--------+-----------------------+
1 8 23
Final floating point:
+-+--------+-----------------------+
|0|10000010|01000011110101110000101|
+-+--------+-----------------------+
1 8 23
What you here do however is not shifting. Shifting means you see the data as a sequence of bits and move them to the left or the right. You do this (usually) regardless of the semantical interpretation of that sequence. Shifts are usually done with the <<
and >>
operators.