Thread: bitwise question

  1. #1
    Registered User
    Join Date
    Nov 2001

    bitwise question

    If I have the first byte of an int set to 00111011, where the last two ones are flags, how do I get the first 6 numbers and then convert them to a number so I can print it out. EX:

    I have 00111011 and I want to get the 001110 out of it, and change that to its own number, without getting/changing the last two ones. I hope this doesn't seem too complicated. Thanks!

  2. #2
    Registered User
    Join Date
    Oct 2001
    If the other bytes of your integer are 0s, then all you need to do is this:

    int a = 0x0000003b;
    int b;
    b = a >> 2; //shift right 2 bits
    cout << hex << b << endl;

    If you need to mask the higher bytes, then do this:
    int a = 0xffffff3b;
    b = (a & 0xff) >> 2;
    cout << hex << b << endl;

    Or use all shifts:
    b = a << 24 >> 26; //shift left 24 bits, then right 26 bits

    This assumes your integer is 4 bytes. To find this out, print out the value of sizeof(int).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise Questions
    By someprogr in forum C Programming
    Replies: 8
    Last Post: 12-14-2008, 06:45 PM
  2. bitwise negation
    By Sathyabodh in forum C Programming
    Replies: 1
    Last Post: 08-12-2003, 09:42 AM
  3. Question...
    By TechWins in forum A Brief History of
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Question, question!
    By oskilian in forum A Brief History of
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM