Results 1 to 8 of 8

Thread: >3 bits set in byte

  1. #1

    Thread Starter
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    >3 bits set in byte

    I need to write a function:

    f( b As Byte ) As Boolean

    The function must return True if the value of b has 3 or less bits set. Otherwise, f returns False.

    I can achieve this by converting b to a string using binary format then counting the number of '1's in the string, but I'd like to know if there's a more elegant way to do it.

    Anyone care to have a go?
    This world is not my home. I'm just passing through.

  2. #2
    New Member
    Join Date
    Apr 2003
    Location
    Beyond Juslibol
    Posts
    9
    Not elegant but can be done in a single line if you know how to translate this from C++ to VB (I don't know)

    Code:
    if (((b>>7)&1+(b>>6)&1+(b>>5)&1+(b>>4)&1+(b>>3)&1+(b>>2)&1+(b>>1)&1+b&1)<=3) return TRUE;
    else
    return FALSE;

  3. #3
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    VB Code:
    1. Function f(b As Byte) As Boolean
    2.  
    3. If ((b \ 128) and 1) + ((b \ 64) and 1) + ((b \ 32) and 1) + ((b \ 16) and 1) + ((b \ 8) and 1) + ((b \ 4) and 1) + ((b \ 2) and 1) + (b and 1)  <= 3 then
    4.   f = True
    5. Else
    6.   f = False
    7. End If
    8.  
    9. End Function

    This should work I think

  4. #4
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking hmm....

    you are testing a boolean statement and if true, you return true, if false, u return false. Why not just return the statement:

    VB Code:
    1. Function f(b As Byte) As Boolean
    2.  
    3. f=((((b \ 128) and 1) + ((b \ 64) and 1) + ((b \ 32) and 1) + ((b \ 16) and 1) + ((b \ 8) and 1) + ((b \ 4) and 1) + ((b \ 2) and 1) + (b and 1))  <= 3)
    4.  
    5. End Function

    Hopefully this should work
    sql_lall

  5. #5
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    private function BOOLINGWOOOO(byval b as byte)
    dim a as byte 'loop variable
    dim c as byte 'number of rights

    For a = 7 to 0 step -1
    if 2^a > b then c = c + 1: b = b - 2^a
    next a


    ^untested, so not sure.
    Don't pay attention to this signature, it's contradictory.

  6. #6
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551

    Re: hmm....

    Originally posted by sql_lall
    you are testing a boolean statement and if true, you return true, if false, u return false.
    I know, but I wanted to stay in the same line as Dr. Luz.

  7. #7

    Thread Starter
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536
    Thanks to all - these look a lot nicer than my string manipulation.
    This world is not my home. I'm just passing through.

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    fast and elegant way:
    y=0!=x&=(x-1);
    y+=0!=x&=(x-1);
    y+=0!=x&=(x-1);
    y+=0!=x&=(x-1);
    return y<4;
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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