Thread: Bits byte

  1. #1
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65

    Bits byte

    If you have a binary number (10101100 - used for bits in a byte) what is the best way to check which position is a 1 or a 0?
    \0

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Take that vaue and AND it with a mask

    10101100 AND 00100000 = 00100000

    10101100 AND 01000000 = 00000000

    If the product is zero, then that bit isnt set

  3. #3
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    Ok, then how come when I run the following code, the binary 10 shows "-----!-!" where - is 0 and ! is 1.

    Code:
    				if(tempBinValue & 10000000) {
    					cout << "!";
    				}else{
    					cout << " ";
    				}
    				if(tempBinValue & 1000000) {
    					cout << "!";
    				}else{
    					cout << " ";
    				}
    				if(tempBinValue & 100000) {
    					cout << "!";
    				}else{
    					cout << " ";
    				}
    				if(tempBinValue & 10000) {
    					cout << "!";
    				}else{
    					cout << " ";
    				}
    				if(tempBinValue & 1000) {
    					cout << "!";
    				}else{
    					cout << " ";
    				}
    				if(tempBinValue & 100) {
    					cout << "!";
    				}else{
    					cout << " ";
    				}
    				if(tempBinValue & 10) {
    					cout << "!";
    				}else{
    					cout << " ";
    				}
    				if(tempBinValue & 1) {
    					cout << "!";
    				}else{
    					cout << " ";
    				}
    				cout << " " << binValue << endl;
    \0

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    You should be using a bitmask not an integer, i.e. instead of 1 use 0x01, instead of 10 use 0x02, 100 - 0x04 1000 - 0x08 10000 - 0x10, 100000 - 0x20, 1000000 - 0x40 and 10000000 - 0x80.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    so, I should be doing something like (tempBitValue & 0x01) instead?
    \0

  6. #6
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    Here is some of my code chopped out:

    Code:
    const unsigned char bit[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
    
    //stuff...
    		 for(int i=0; i < (imageWidth * imageLength); i++)
    			{
    			int binValue = binaryConv(buf[i]);
    			int tempBinValue = binValue;
    			for(int j = 0; j < 8; j++)
    			{
    				if(tempBinValue & bit[j])
    				{
    					cout << "!";
    				}else{
    					cout << " ";
    				}
    			}
    				
    				cout << " " << binValue << endl;
    
    			if((i % imageWidth) == 1) cout << endl;
    		 }
    For a line of 11111111 it's returning 11000111 when I run this... what's going on? It should return 11111111.
    \0

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    This worked fine for me, dont have your buffer to test it on though.
    Code:
    const unsigned char bit[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
    int tempBinValue = 255;
    
    for(int j = 0; j < 8; j++)
    {
       if(tempBinValue & bit[j])
       {
          cout << "1 ";
       }
       else
       {
          cout << "0 ";
       }
    }
    cout << endl;
    Btw, why do you do
    Code:
    int binValue = binaryConv(buf[i]);
    int tempBinValue = binValue;
    and not just,

    Code:
    int tempBinValue = binaryConv(buf[i]);

    When your code returns 11000111 what is the binValue then ?

  8. #8
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    Yeah the
    Code:
    int binValue = binaryConv(buf[i]);
    int tempBinValue = binValue;
    was for something else, but I changed it to
    Code:
    int tempBinValue = binaryConv(buf[i]);
    now. Thanks.

    When it returns 11000111 binValue = 11111111

    It's very strange... only 0 and a few others work, but others don't. I don't understand why some do and some don't.
    \0

  9. #9
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    Ok, here is my full code. The part between the //---- s is most likely what is going to need attention. But I included the rest incase I'm overlooking something else.

    Code:
    int bitonal() {
    	uint32 imageWidth, imageLength;
    	int stripMax, stripCount;
    	unsigned long imageOffset, result, bufferSize;
    	tsize_t stripSize;
    
    	char* fileName = "10x10bi.tif";
        TIFF* tif = TIFFOpen(fileName, "r");
    
        if (tif) {
    
    		TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &imageWidth);
    		TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &imageLength);
    		
    		m_memoryWidth = (imageWidth + (1/2)) / 8;
    		m_memoryLength = imageLength;
    
    		// Read in the possibly multiple strips
    		stripSize = TIFFStripSize(tif);
    		stripMax = TIFFNumberOfStrips(tif);
    		imageOffset = 0;
      
    		bufferSize = m_memoryWidth * m_memoryLength;
    		m_bufferSize = bufferSize;
    		
    		unsigned char* buf = new unsigned char[bufferSize];
    
    		const unsigned int bit[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
    
    		for (stripCount = 0; stripCount < stripMax; stripCount++){
    			if((result = TIFFReadEncodedStrip (tif, stripCount,
    				buf + imageOffset,
    				stripSize)) == -1){
    				fprintf(stderr, "Read error on input strip number %d\n", stripCount);
    				exit(42);
    			}
    
    			imageOffset += result;
    		}
    
    //------------------------ERRORS?
    		 for(int i=0; i < (imageWidth * imageLength); i++)
    			{
    			int binValue = binaryConv(buf[i]);
    			for(int j = 0; j < 8; j++)
    			{
    				if(binValue & bit[j])
    				{
    					cout << 1;
    				}else{
    					cout << 0;
    				}
    			}
    				
    			cout << " " << binValue << endl;
    		 }
    //-------------------------
    	}
    	return 0;
    }
    Here is another part of the code you might want to look at (just in case it's got something wrong with it -- but I don't think it does) This is the part where it converts a decimal number to binary.

    Code:
    int binaryConv(int number) {
    	int result = 0;
    
    ////////////////////////////////////////////////////////////////////
    //	Input
    ////////////////////////////////////////////////////////////////////
    
    	int numCopy = number;
    
    	if (number > 255 || number < 0) {
    		cout << "ERROR: Number cannot be greater than 255 or less than 0." << endl;
    		return 0;
    	}
    
    ////////////////////////////////////////////////////////////////////
    //	Calculates the binary value of the input.
    //	Works by looping through and taking away the already stored
    //		binary numbers.
    ////////////////////////////////////////////////////////////////////
    
    	if (number % 2) {
    		result = 1;
    		numCopy--;
    	}
    	
    	while (numCopy != 0) {
    		if(numCopy / 128) {
    			result = result + 10000000;
    			numCopy = numCopy - 128;
    		}
    		else if(numCopy / 64) {
    			result = result + 1000000;
    			numCopy = numCopy - 64;
    		}
    		else if(numCopy / 32) {
    			result = result + 100000;
    			numCopy = numCopy - 32;
    		}
    		else if(numCopy / 16) {
    			result = result + 10000;
    			numCopy = numCopy - 16;
    		}
    		else if(numCopy / 8) {
    			result = result + 1000;
    			numCopy = numCopy - 8;
    		}
    		else if(numCopy / 4) {
    			result = result + 100;
    			numCopy = numCopy - 4;
    		}
    		else if(numCopy / 2) {
    			result = result + 10;
    			numCopy = numCopy - 2;
    		}
    		else {
    			cout << "ERROR: Invalid number." << endl;
    			return 0;
    		}
    	}
    
    ////////////////////////////////////////////////////////////////////
    //	Output
    ////////////////////////////////////////////////////////////////////
    
    	return result;
    }
    \0

  10. #10
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    Here is a simpler version of what I'm trying to do...

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() {
    	int val = 255;
    	int result = 0;
    	const unsigned int mask[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
    	
    	for (int nBit = 0; nBit < 8; nBit++) {
    		result = ((val & mask[nBit]) >> nBit);
    		cout << result << endl;
    	}
    	return(0);
    }
    But even THIS doesn't work. Result should output: 128, 64, 32, 16, 8, 4, 2, 1. But it skips 64, 16, 4, and 1. Any suggestions?

    Actual output:
    Code:
    128
    32
    8
    2
    0
    0
    0
    Last edited by Azmeos; 07-10-2003 at 11:17 AM.
    \0

  11. #11
    Registered User
    Join Date
    May 2003
    Posts
    161
    Here is another part of the code you might want to look at (just in case it's got something wrong with it -- but I don't think it does) This is the part where it converts a decimal number to binary.
    So you're converting a decimal number into a decimal representation of its binary number... and then trying to use bitmasks to print the binary number?

    That won't work, and I don't think it's what you want. Try cutting out the binaryConv() function altogether, and see if that works better for you.


    [edit]
    I'm not sure if I was clear enough. The number is already stored in memory in binary format, so there is no need for any conversion.

    Code:
    //------------------------ERRORS?
      for(int i=0; i < (imageWidth * imageLength); i++)
     {
    
        int binValue = binaryConv(buf[i]);
    
    
        for(int j = 0; j < 8; j++)
       {
    
        // this should be buf[i], NOT binValue....
        // if(binValue & bit[j])
        if(buf[i] & bit[j])
        {
          cout << 1;
        }else{
          cout << 0;
        }
      }
    				
      cout << " " << binValue << endl;
    		 }
    //-------------------------


    [/edit]
    Last edited by thefroggy; 07-10-2003 at 11:52 AM.

  12. #12
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    This should work, without the shift right.
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() {
    	int val = 255;
    	int result = 0;
    	const unsigned int mask[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
    	
    	for (int nBit = 0; nBit < 8; nBit++) {
    		result = (val & mask[nBit]);
    		cout << result << endl;
    	}
    	return(0);
    }

  13. #13
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    how about something like:
    Code:
     int binconvert(int num)
     {
         int x=0;
     for (int i=0;i<8;i++)
     {
      if (num&mask[i])
            x+=mask2[i]; //where mask2 holds 10000000,1000000,...
      }
      return x;
     }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  14. #14
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    Thanks everyone, with your help I figured it out. Now it works, the binary is converted properly. Now I just have to figure out how to get it to read up to a non-multiple of 8 ... Say I have a 10x10 image. What's the best way to have it read all 100 bits, but not go over (or under)?

    Newest code:

    Code:
    		int binValue = 0;
    		int count = 0;
    		for(int i=0; i < ((imageWidth * imageLength) / 8); i++)
    		{
    			for(int j = 0; j <= 7; j++)
    			{
    				binValue = (buf[i] & bit[j]);
    				if(binValue == 0) cout << 0;
    				if(j == 0 && binValue == 128){
    					cout << 1;
    				}
    				if(j == 1 && binValue == 64){
    					cout << 1;
    				}
    				if(j == 2 && binValue == 32){
    					cout << 1;
    				}
    				if(j == 3 && binValue == 16){
    					cout << 1;
    				}
    				if(j == 4 && binValue == 8){
    					cout << 1;
    				}
    				if(j == 5 && binValue == 4){
    					cout << 1;
    				}
    				if(j == 6 && binValue == 2){
    					cout << 1;
    				}
    				if(j == 7 && binValue == 1){
    					cout << 1;
    				}
    				count++;
    				if((count % imageWidth) == 1 && count != 1 || imageWidth == 1) cout << endl;
    			}
    		}
    	}
    Last edited by Azmeos; 07-10-2003 at 12:42 PM.
    \0

  15. #15
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    I'm curious as to why you are using "bit[j]" when all it does is increase execution time and required ram space. Why not calculate this array in realtime as you need it ?

    Eg.

    Instead of this:

    Code:
    for(int j = 0; j <= 7; j++)
    {
       binValue = (buf[i] & bit[j]);
    do this:

    Code:
    for(int j = 0; j <= 7; j++)
    {
       binValue = (buf[i] & (1<<j));
    I wonder also about your large if statement, isn't the following easier?

    Code:
    for(int j = 0; j <= 7; j++)
    {
       binValue = (buf[i] & (1<<j));
       if (binValue) cout << 1; else cout << 0;
    }
    Last edited by _Elixia_; 07-10-2003 at 02:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 03:03 AM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. byte is equal 8 bits???
    By Micko in forum C Programming
    Replies: 3
    Last Post: 10-15-2004, 10:12 AM
  5. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 05:46 PM