Bitwise Questions

This is a discussion on Bitwise Questions within the C Programming forums, part of the General Programming Boards category; I'm trying to optimize my code and I have two bitwise questions: I have a variable i. I am running ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    51

    Bitwise Questions

    I'm trying to optimize my code and I have two bitwise questions:

    I have a variable i. I am running a bitwise operation. If var1 = 0, then i = 1, if var1 = any other number, then i = 0. Would this work: i = ~(var1 & 0).

    Heres my second operation:

    I have a variable i. I am running a bitwise operation. If var1's 7th bit (its 0-7) = 1, then i = 128. if var1's 7th bit = 0 then i = 0.

    I'm trying to avoid using if statements and am having issues with bitwise operations. Any help would be appreciated.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    (var1 & 0) is always zero, so it won't do what you ask for.

    Code:
    !var1
    will give the result you want. Of course, this is not a bitwise operator, but it's most likely possible to solve (in the compiled code) without branches, which I presume is your actual goal - whethter you use if-statements or not shouldn't be a goal in optimization, but avoiding branches/jumps certainly SHOULD be.

    The second one can be solved with a simple and-statement - I'll let you post your attempt first - because that way, you will LEARN SOMETHING more than "I can post something and get an answer that I can paste into my code" - I think you know how to do the latter already.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by someprogr View Post
    i = var1 | 128 ?
    Nope. Use AND, not OR.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    51
    Quote Originally Posted by matsp View Post
    Nope. Use AND, not OR.

    --
    Mats
    actually, wouldnt shifting it to the right and isolating it be faster? i.e. i = var1 >> 7
    Last edited by someprogr; 12-14-2008 at 05:38 PM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by someprogr View Post
    actually, wouldnt shifting it to the right and isolating it be faster?
    Not if you want to have i = 128 when var1 has the bit no 7 set. If you want i = 1 when bit 7 is set, then shifting it would be the right thing to do. Shifting is never faster than and, but it is slower on at least older/smaller processors - modern high-end processors, and the result is probably the same amount of time to shift or mask the bit.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    51
    Quote Originally Posted by matsp View Post
    Not if you want to have i = 128 when var1 has the bit no 7 set. If you want i = 1 when bit 7 is set, then shifting it would be the right thing to do. Shifting is never faster than and, but it is slower on at least older/smaller processors - modern high-end processors, and the result is probably the same amount of time to shift or mask the bit.

    --
    Mats
    oh ok. thanks

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,049
    bitwise AND'ing with octal 0100 ought to do the job as in
    Code:
    i = (var1 & 0100) ? 128 : 0;

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by itCbitC View Post
    bitwise AND'ing with octal 0100 ought to do the job as in
    Code:
    i = (var1 & 0100) ? 128 : 0;
    Ehm, yes, if bit 7 is the bit worth 64, then I would agree that this would achieve the desired effect - but the terniary operator is often implmented as a if-statement, so may lead to a branch, so if this is REALLY what you want to do, then:
    Code:
    i = (var1 & 64) << 1;
    will achieve the same thing, guaranteed to be without a branch.

    Using octal will achieve nothing special here.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,049
    Yes the ternary operator is a contraction of the if-else; the octal's only for the effect; and i = (var1 & 64) << 1; is a clever one

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 09:47 AM
  2. 2 bitwise questions
    By kirtikjr in forum C Programming
    Replies: 4
    Last Post: 12-04-2008, 08:08 AM
  3. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 06:36 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. Bitwise Test Questions
    By Mister C in forum C Programming
    Replies: 9
    Last Post: 11-27-2002, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21