Find first and last set bit of a long integer: Difference between revisions

Content added Content deleted
m (put Quackery entry in correct alphabetic order. D'oh.)
No edit summary
Line 819: Line 819:
717368321110468608 0000100111110100100110101010111111110000111010000110100000000000 MSB: 59 LSB: 11
717368321110468608 0000100111110100100110101010111111110000111010000110100000000000 MSB: 59 LSB: 11
</pre>
</pre>


=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn IntegerToBinaryStr( x as NSInteger ) as CFStringRef
CFStringRef resultStr = @""
while ( x )
resultStr = fn StringByAppendingString( fn StringWithFormat( @"%lu", x && 1 ), resultStr )
x = x >> 1
wend
end fn = resultStr

local fn FirstAndLastBit
NSInteger i, p = 1
for i = 0 to 11
CFStringRef binaryStr = fn IntegerToBinaryStr(p)
printf @"%20lld %-62s MSB: %2lld LSB: %2lld", p, fn StringUTF8String( binaryStr ), len( binaryStr ) - 1, i
p = p * 42
next
end fn

fn FirstAndLastBit

HandleEvents

</syntaxhighlight>
{{output}
<pre>
1 1 MSB: 0 LSB: 0
42 101010 MSB: 5 LSB: 1
1764 11011100100 MSB: 10 LSB: 2
74088 10010000101101000 MSB: 16 LSB: 3
3111696 1011110111101100010000 MSB: 21 LSB: 4
130691232 111110010100011000010100000 MSB: 26 LSB: 5
5489031744 101000111001010111111101001000000 MSB: 32 LSB: 6
230539333248 11010110101101001101110000111010000000 MSB: 37 LSB: 7
9682651996416 10001100111001101011000010000110000100000000 MSB: 43 LSB: 8
406671383849472 1011100011101110110001111010111111110101000000000 MSB: 48 LSB: 9
17080198121677824 111100101011100101100110000101101111000110010000000000 MSB: 53 LSB: 10
717368321110468608 100111110100100110101010111111110000111010000110100000000000 MSB: 59 LSB: 11
</pre>