-
One bit
How do I find out in a very fast way if exactly one bit of a byte is set?
I currently use this (inline assembly):
Code:
mov al, bit_depth ; bit_depth is what I want to test
shift:
shr al
jnc shift
test al, al
jz just_one_bit
Anyone got a better idea?
-
Here's another piece I've come up with:
Code:
movzx ax, bit_depth
bsf bx, ax
bsr cx, ax
cmp bx, cx
je just_one_bit
With the advantage that it doesn't need any jumps.
-
-
Yeah, keda gave me that piece by jim:
Code:
bool just_one_bit = (to_test & (to_test-1)) == 0;
-
I know that the Z80 has a BIT command, don't know if x86 does though...
Code:
BIT [BitNum],[What]
For the Z80... I think. Maybe parms are backwards.
-
I don't think the 80x86 instruction set contains a BIT instruction.
Also, what would the code segment look like in this problem?
-
Why don't you and your byte with 00010000 (the one in the location you want your bit tested) and chek if the result is zero?
(just a thought)
-
It depends if this is a check to see if this is a certain bit or any bit.
-
No, this is for any single bit, not matter where.
So, acceptable for a byte would be
00000001
00000010
00000100
00001000
00010000
00100000
01000000
10000000
-
Isn't bsf and bsr sorta expensive? I think the first alogrithm is faster.
-
The third (the line of C code) is the fastest.
-
What would the assembly look like on that one?
-
Code:
mov eax, to_test
dec eax
and eax, to_test
jz just_one_bit