|
-
Feb 15th, 2003, 06:11 AM
#1
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?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 15th, 2003, 06:39 AM
#2
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 15th, 2003, 04:03 PM
#3
Member
-
Feb 15th, 2003, 04:32 PM
#4
Yeah, keda gave me that piece by jim:
Code:
bool just_one_bit = (to_test & (to_test-1)) == 0;
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Apr 25th, 2003, 07:55 PM
#5
Lively Member
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.
-AndySoft
[email protected]
[email protected] (Use the other one first!)
I use: NASM, HTML, Perl, PHP, JavaScript, Batch, TI-Basic (TI-83+), QBasic 1.1, QuickBASIC 4.5, QuickBASIC Extended 7.1, VB-WIN 6 Ent., and the rest of Visual Studio 6 Ent.  And who knows what else!
-
Jun 18th, 2003, 06:19 PM
#6
Fanatic Member
I don't think the 80x86 instruction set contains a BIT instruction.
Also, what would the code segment look like in this problem?
-
Jul 6th, 2003, 09:55 PM
#7
New Member
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)
-
Jul 6th, 2003, 10:44 PM
#8
Fanatic Member
It depends if this is a check to see if this is a certain bit or any bit.
"Can't" and "shouldn't" are two totally separate things.
All questions should be answered. All answers should be true. That is why I post.
-
Jul 7th, 2003, 01:04 AM
#9
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
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jul 8th, 2003, 11:34 PM
#10
Fanatic Member
Isn't bsf and bsr sorta expensive? I think the first alogrithm is faster.
"Can't" and "shouldn't" are two totally separate things.
All questions should be answered. All answers should be true. That is why I post.
-
Jul 9th, 2003, 01:12 AM
#11
The third (the line of C code) is the fastest.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jul 9th, 2003, 04:03 PM
#12
Fanatic Member
What would the assembly look like on that one?
"Can't" and "shouldn't" are two totally separate things.
All questions should be answered. All answers should be true. That is why I post.
-
Jul 10th, 2003, 01:38 AM
#13
Code:
mov eax, to_test
dec eax
and eax, to_test
jz just_one_bit
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|