Thread: Bitwise operations on unsigned character

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    Bitwise operations on unsigned character

    A few months ago, I picked up a very useful function for outputting a binary string onto the screen.

    Code:
    void bits_uint(int value)
    {
       int bit;
       for ( bit = (~0U >> 1) + 1; bit > 0; bit >>= 1 )
       {
          cout << (value & bit ? '1' : '0');
       }
       cout << endl;
    }
    This time, I wanted to make it a template function because I thought it would be neat, and because I wouldn't have to define a different function for each argument type:

    Code:
    template<class T>
    void bits_uint(T value)
    {
       T bit;
       for ( bit = /* msb */(~0 >> 1) + 1; bit > 0; bit >>= 1 )
       {
          cout << (value & bit ? '1' : '0');
       }
       cout << endl;
    }
    When I tried implementing the function as one of type
    Code:
    bit_uint<unsigned char>(whatever)
    it did nothing because the bit was assigned a 0. My guess is, this happens because Intel CPUs are little endian, and the most significant bit is actually the 32nd and not the 8th as one would expect in a character.
    Would there be a way to create a function which could print the binary string for any native type in c++? For example, float, double, char, etc.?

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Look up <bitset>

    Here are a few code fragments...
    Code:
    #include <bitset>   
    using namespace std;
    
    int X, Bit, Mask;
    const int MAX_BITS = 8;     // Display one byte with bitset 
    
    cout << "\t X    = " << hex << X << "  =\t" << bitset<MAX_BITS>(X) << endl;
    Little endian and big indian are for file I/O. In memory, you have the usual bits 0 thru 31. (A type char may be 32 bits in a Windows system... I don't remember right now.... You can't physically address anything smaller on a 32-bit data bus, but Windows may "pack" the data using virtual memory.)

    I don't think the bitwise operators are valid on floats or doubles. If they do work, it's up to you to understand how floating point numbers are stored in binary memory, on your particular system.


    FYI - In the "real world", nobody uses actual binary I/O. In the hardware world, we work with individual bits every day, and we use hexadecimal.

    - It's easy to convert between hex and binary. You can learn to do binary-hex conversions in your head. (Much easier than binary-decimal conversion!)
    - Hex is easier than binary to read, write, and speak.
    - Hex is built into cin and cout.
    Last edited by DougDbug; 05-02-2006 at 08:19 PM.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Thanks. Guess I won't have to implement my unruly function anymore.

    BTW, I got my function to work. Seems I should have split the assignment into separate lines. Working with integers on the right side and an unsigned character on the left would have simply assigned an integer to the l-value. How stupid of me.

    Code:
    template<class T>
    void bits_uint(T value)
    {
       T bit = ~0;
       bit >>= 1;
       bit++;
       for ( ; bit > 0; bit >>= 1 )
       {
          cout << (value & bit ? '1' : '0');
       }
       cout << endl;
    }

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    I needed to know the interior workings of a class I was making.

    I had some private integer variables defined in bitfields. Then, I wanted to use bitwise operations to compare two classes.

    For the curious, here's my class:
    Code:
    class Phoneme{
    private:
    	UINT alveolar		:1;
    	UINT bilabial		:1;
    	UINT continuant		:1;
    	UINT glottal		:1;
    	UINT labiodental	:1;
    	UINT lateral		:1;
    	UINT nasal			:1;
    	UINT plosive		:1;
    	UINT tap			:1;
    	UINT trill			:1;
    	UINT velar			:1;
    	UINT voiced			:1;
    	UINT vocalic		:1;	
    public:
    	Phoneme(){}
    	~Phoneme(){}
    	void set_bilabial(int i = 1){bilabial = i;}
    	void set_continuant(int i = 1){continuant = i;}
    	void set_glottal(int i = 1){glottal = i;}
    	void set_labiodental(int i = 1){labiodental = i;}
    	void set_lateral(int i = 1){lateral = i;}
    	void set_nasal(int i = 1){nasal = i;}
    	void set_plosive(int i = 1){plosive = i;}
    	void set_tap(int i = 1){tap = i;}
    	void set_trill(int i = 1){trill = i;}
    	void set_velar(int i = 1){velar = i;}
    	void set_vocalic(int i = 1){vocalic = i;}	
    	void set_voiced(int i = 1){voiced = i;}
    	virtual void initialize(){bilabial = continuant = glottal = labiodental = lateral =
    		nasal = plosive = tap = trill = velar = vocalic = voiced = 0;}
    	friend operator-(Phoneme& p1, Phoneme& p2);
    };
    Still in the working stages.

    One question about the bitset class or function or whatever it is.

    I tried the following code but it gave me an error message:
    Code:
    Phoneme b;
    ... //initialize this and that
    cout << bitset<MAX_BITS>(*(unsigned int*)(&b)) << endl;
    but the following does:
    Code:
    cout << bitset<MAX_BITS>((*(unsigned int*)(&b))) << endl;
    Why does giving it an extra set of parentheses make it work? I mean, wouldn't the dereference operator have precedence over whatever it was bitset was doing?

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by cunnus88
    One question about the bitset class or function or whatever it is.

    I tried the following code but it gave me an error message:
    Code:
    Phoneme b;
    ... //initialize this and that
    cout << bitset<MAX_BITS>(*(unsigned int*)(&b)) << endl;
    but the following does:
    Code:
    cout << bitset<MAX_BITS>((*(unsigned int*)(&b))) << endl;
    Why does giving it an extra set of parentheses make it work? I mean, wouldn't the dereference operator have precedence over whatever it was bitset was doing?
    What error message?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Please
    By YouShallNotPass in forum C++ Programming
    Replies: 3
    Last Post: 08-22-2006, 11:22 AM
  2. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM
  3. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 02:13 PM
  4. ANY BODY WILLING TO HELP ME WITH Microsoft Visual C++
    By BiG pImPiN fOoL in forum C++ Programming
    Replies: 12
    Last Post: 11-04-2001, 06:03 PM
  5. bitwise operations
    By bukko in forum C Programming
    Replies: 3
    Last Post: 10-06-2001, 06:56 AM