Results 1 to 13 of 13

Thread: One bit

  1. #1

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  2. #2

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  3. #3
    Member AirScape17's Avatar
    Join Date
    Aug 2002
    Location
    England
    Posts
    34
    how about and'ing it?

  4. #4

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  5. #5
    Lively Member AndySoft's Avatar
    Join Date
    Oct 2000
    Location
    Massillon, OH
    Posts
    68
    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!

  6. #6
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    I don't think the 80x86 instruction set contains a BIT instruction.

    Also, what would the code segment look like in this problem?

  7. #7
    New Member
    Join Date
    Apr 2003
    Location
    here and there
    Posts
    2
    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)

  8. #8
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    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.

  9. #9

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  10. #10
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    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.

  11. #11

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  12. #12
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    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.

  13. #13

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width