Thread: Help with determining bit values

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    44

    Help with determining bit values

    Hello,
    I am reading 5 words from memory and then trying to determine bit values within each of the words.

    I am given the location of the bit positions in hex, so can I bitwise operator "and" the word and the hex value to determine the value of the bit position?

    For example:

    is_bit_set = word_read_from_mem & 0x00000001;
    is_bit_set = word_read_from_mem & 0x00080000;

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It sounds like you answered your own question
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2016
    Posts
    6
    Code:
    int i = 0b1101101;
    printf("%d\n",  (i & (1 << 4))  );
    this prints ' 0 ' because the fourth bit from the left is 0.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by wrvc View Post
    Code:
    int i = 0b1101101;
    printf("%d\n",  (i & (1 << 4))  );
    this prints ' 0 ' because the fourth bit from the left is 0.
    ... assuming that a language extension is enabled allowing 0b1101101 to be a valid constant rather than a syntax error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2016
    Posts
    6
    Quote Originally Posted by laserlight View Post
    ... assuming that a language extension is enabled allowing 0b1101101 to be a valid constant rather than a syntax error.
    why would someone not use gcc?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by wrvc
    why would someone not use gcc?
    Because gcc is not the only C compiler available: if you wish to discuss the merits or lack thereof of various C compilers vis-a-vis gcc, start a new topic. knik653 did not state any particular compiler, so it is not right to provide non-standard code without noting the fact.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2016
    Posts
    6
    Quote Originally Posted by laserlight View Post
    Because gcc is not the only C compiler available: if you wish to discuss the merits or lack thereof of various C compilers vis-a-vis gcc, start a new topic. knik653 did not state any particular compiler, so it is not right to provide non-standard code without noting the fact.
    OK but you should still not the binary literal was just a visual example

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 07-27-2013, 10:20 AM
  2. Replies: 2
    Last Post: 07-21-2013, 10:35 PM
  3. write() and read() int values from socket returning wrong values.
    By Premjith P S in forum Linux Programming
    Replies: 8
    Last Post: 11-29-2012, 02:59 PM
  4. Determining size of array returning strange values?
    By edddo in forum C++ Programming
    Replies: 13
    Last Post: 07-28-2011, 03:37 AM
  5. Determining values on a web page
    By AaA in forum C Programming
    Replies: 1
    Last Post: 06-28-2005, 04:47 AM

Tags for this Thread