[chbot] !!!!!!!! Choc fish challenge 2 !!!!!!!!!
Paul Davey
plmdvy at gmail.com
Tue Aug 2 12:08:50 BST 2011
> On 8/2/11, Michael Field <hamster at snap.net.nz> wrote:
>> It it Tuesday 10pm and nobody has answered?
>>
>> I think function(x) returns the the first power of two that is greater
>> than or equal to x.
>>
>> It works because the first "shift and or" propagates the First Set Bit
>> (FSB) to bit(FSB-1)
>> The second "shift and or" propagates bits(FSB) and bit(FSB-1) to
>> bit(FSB-2) and and(FSB-3)
>> The third "shift and or" propagates bit(FSB) through bit(FSB-3) to
>> bits(FSB-4) through bits(FSB-7)
>> .... and so on.
>>
>> The "x--" at the start makes sure that it returns "greater than or equal
>> to" x. If you remove it you get the first power of two greater than x.
>> e.g. function(256) will return 512.
>>
>> But I don't think it works if x > 2^31, as for f(0x80000001) the return
>> value will be 0xFFFFFFFF+1 = 0 <?>.
I think that both signed and unsigned integer overflow is undefined
behaviour in the C standard, thus if it happens to be 0 fine but it
doesn't have to be.
>>
>> Without the "x++" on the return it would be quite handy to light a
>> string of LEDs to give a sort of give a log(2) signal level meter....
>>
>> Mike
>>
More information about the Chchrobotics
mailing list