Thread: Bitwise Questions

Hybrid View

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

  2. #2
    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.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    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, 08: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, 05: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