Thread: bit manipulation

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    bit manipulation

    Hi,

    I would like to do the following:

    (i) from a 24bit hexadecimal mask, I would like to know which bit in ON (1).

    (ii) from a 16 bit data word I would like to read the 12 bits (starting from LSB) and 13-16 bits separately


    How do I do this?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What DO YOU think?

    --
    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
    Nov 2006
    Location
    japan
    Posts
    126

    Wink

    for masking bits... why don't use exclusive disjunction? (if c does not implement it it could be easily implemented with logic or "|" and logic and "&" operators)

    your could compare your result with something like pow(2,n) to know if right n-most bit is on or not.

    Regards
    Mac OS 10.6 Snow Leopard : Darwin

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    anybody has a piece of code?

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you're a beginner then you'll learn more by working out the answer yourself, or at the very least looking it up yourself online.
    If not, then you'll already know how to do it.

    We don't help people cheat if that's what you're after.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    Thanks!!

    But I thought having a piece of simple code to look at is a process of learning and posting a question in a forum is the way to learn from internet.

    But anyway, thanks again for redefining "learning" and "cheating".

    Be happy always!

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, there was two purposes to my original "what do you think":
    1. How much do you actually know about the subject.
    2. You need to learn by doing, not by copying.

    I may very well be able to post a piece of example code, but without knowing the level of your understanding, the example code may not be meaningful to you.

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

  8. #8
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Quote Originally Posted by matsp View Post
    I may very well be able to post a piece of example code, but without knowing the level of your understanding, the example code may not be meaningful to you.
    Let's have a test:

    Code:
    ((1 << b) - 1) << c
    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dsupriya View Post
    Thanks!!

    But I thought having a piece of simple code to look at is a process of learning and posting a question in a forum is the way to learn from internet.

    But anyway, thanks again for redefining "learning" and "cheating".

    Be happy always!
    Posting a question in a forum is indeed a way to learn from the internet.

    Note the complete absence of "having someone write your code for you".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 32 bit to 64 bit Ubuntu
    By Akkernight in forum Tech Board
    Replies: 15
    Last Post: 11-17-2008, 03:14 AM
  2. bit value check efficiency
    By George2 in forum C Programming
    Replies: 5
    Last Post: 11-05-2007, 07:59 AM
  3. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  4. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 08:02 PM
  5. Copy bit to bit
    By Coder2Die4 in forum C Programming
    Replies: 15
    Last Post: 06-26-2003, 09:58 AM