Thread: reading certain bits from a byte

  1. #1
    Unregistered
    Guest

    reading certain bits from a byte

    pretend my char is 85 (01010101)
    if I only want to read the last 3 bits (010...) from the char and use it as a separate value, how do I do it?
    since 010 the result would be 2, I know the value of the bits are found at 0x80, 0x40, and 0x20.
    any one know how to do this?

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Combination of bit masking and shifting.

    Code:
    #include <iostream>
    
    int main() {
    	int test=85;
    	std::cout << ((test & (0x80 | 0x40 | 0x20)) >> 5) << std::endl;
    	return 0;
    }
    0x80 | 0x40 | 0x20 is 0xE0, just shown like that to see where it comes from.
    Last edited by SilentStrike; 07-19-2002 at 02:08 PM.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User fletch's Avatar
    Join Date
    Jul 2002
    Posts
    176

    hmmm....

    SilentStrike,

    How does '>> 5' work?

    I understand what you're doing up the point of >> 5. The first part, (test & (0x80 | 0x40 | 0x20)), should return 01000000b. Judging by the output, I'm guessing that '>> 5' shifts everything to the right by 5 bits.

    fletch
    "Logic is the art of going wrong with confidence."
    Morris Kline

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You can use bitfields to manipulate specific bits:

    Code:
    #include <iostream>
    
    using namespace std;
    
    typedef struct
    {
        unsigned int bit0:1;
        unsigned int bit1:1;
        unsigned int bit2:1;
        unsigned int bit3:1;
    
        unsigned int bit4:1;
        unsigned int bit5:1;
        unsigned int bit6:1;
        unsigned int bit7:1;
    } bitfield_s;
    
    typedef union
    {
        bitfield_s bitfield;
        unsigned char byte;
    } byte_u;
    
    int main ()
    {
        byte_u var;
    
        var.byte = 0xBA;
    
        cout << "bit 0: " << var.bitfield.bit0 << endl;
        cout << "bit 6: " << var.bitfield.bit6 << endl;
        
        return 0;
    }

  5. #5
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    I understand what you're doing up the point of >> 5. The first part, (test & (0x80 | 0x40 | 0x20)), should return 01000000b. Judging by the output, I'm guessing that '>> 5' shifts everything to the right by 5 bits.
    That's exactly what it does.

  6. #6
    Unregistered
    Guest
    okay, but what if I only want to read the first 3 bits of the byte (0x01, 0x02, and 0x04) and use it as a separate value?

  7. #7
    Registered User fletch's Avatar
    Join Date
    Jul 2002
    Posts
    176
    Code:
    int test1, test2;
    
    test1 = 85;
    test2 = ((test1 & 0xE0) >> 5);
    
    std::cout << "test1 = " << test1 << std::endl;        // 85
    std::cout << "test2 = " << test2 << std::endl;        // 2
    "Logic is the art of going wrong with confidence."
    Morris Kline

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a byte and its bits to a function
    By rtarbell in forum C Programming
    Replies: 9
    Last Post: 12-04-2008, 09:24 AM
  2. Newbie sockets question/problem
    By (Slith++) in forum Networking/Device Communication
    Replies: 6
    Last Post: 12-06-2007, 12:23 AM
  3. Reading A Byte From A Certain Offset
    By 4point5 in forum Windows Programming
    Replies: 1
    Last Post: 10-31-2002, 06:36 PM
  4. Bits & Bytes
    By C of Green in forum C++ Programming
    Replies: 8
    Last Post: 06-21-2002, 06:50 PM
  5. sorting a structure of arrays
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 03-15-2002, 11:45 AM