Reputation: 257
I am multiplying a 32-bit number with the same number to get their power of 2. Then I want to get certain bits and leave the rest, but the result gets out rounded and not exact.
For example, what I want to calculate fec2b8f7 × fec2b8f7 = fd86fb26fffffe51
(in hexadecimal notation), in MATLAB it comes out like this:
>> (((uint64(hex2dec('fec2b8f7'))) * (uint64(hex2dec('fec2b8f7')))))
ans =
fd86fb26fffffe51
Which is correct. From this 64-bit hexadecimal number I want to get highest 34 bits out of it, so to get first 34 bits out of 64 bits you need to get rid of the last 30 bits, but division by 2:
>> (((uint64(hex2dec('fec2b8f7'))) * (uint64(hex2dec('fec2b8f7'))))) / uint64(2^30)
ans =
00000003f61bec9c
Sadly the result is not correct, as it was rounded. The correct result should be 00000003f61bec9b.
Is there a way to just get 34 bits out of 64 bit number without rounding? I tried floor
, but it doesn't work until the division operation is done, which gets an incorrect answer.
Upvotes: 2
Views: 192