Find first and last set bit of a long integer: Difference between revisions
Find first and last set bit of a long integer (view source)
Revision as of 11:04, 11 October 2022
, 1 year agoAdded Quackery.
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
(Added Quackery.) |
||
Line 1,641:
(19 19 102))
</syntaxhighlight>
=={{header|Quackery}}==
Quackery numbers are BigInts.
<code>lsb</code> returns <code>-1</code> if passed <code>0</code>, which has no bits set.
<code>msb</code> returns <code>-1</code> if passed a negative number, which has no highest set bit, or <code>0</code>, which has no bits set.
The reverse functions <code>rlwb</code> and <code>rupb</code> are not meaningful for BigInts as they do not have a rightmost bit. (Well, they do because memory is finite, but the size limit is not fixed.)
<syntaxhighlight lang="Quackery"> [ dup 0 = iff
[ 1 - ] done
0 swap
[ dup 1 & not while
dip 1+
1 >>
again ]
drop ] is lsb ( n --> n )
[ -1 swap
[ dup 1 < not while
dip 1+
1 >>
again ]
drop ] is msb ( n --> n )
6 times
[ 42 i^ ** dup echo
say " msb:"
dup msb echo
say " lsb:"
lsb echo cr ]
cr
6 times
[ 1302 i^ ** dup echo
say " msb:"
dup msb echo
say " lsb:"
lsb echo cr ]</syntaxhighlight>
{{out}}
<pre>1 msb:0 lsb:0
42 msb:5 lsb:1
1764 msb:10 lsb:2
74088 msb:16 lsb:3
3111696 msb:21 lsb:4
130691232 msb:26 lsb:5
1 msb:0 lsb:0
1302 msb:10 lsb:1
1695204 msb:20 lsb:2
2207155608 msb:31 lsb:3
2873716601616 msb:41 lsb:4
3741579015304032 msb:51 lsb:5</pre>
=={{header|Raku}}==
|