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?
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
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
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
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.
so, I should be doing something like (tempBitValue & 0x01) instead?
\0
Here is some of my code chopped out:
For a line of 11111111 it's returning 11000111 when I run this... what's going on? It should return 11111111.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; }
\0
This worked fine for me, dont have your buffer to test it on though.
Btw, why do you doCode: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;
and not just,Code:int binValue = binaryConv(buf[i]); int tempBinValue = binValue;
Code:int tempBinValue = binaryConv(buf[i]);
When your code returns 11000111 what is the binValue then ?
Yeah the
was for something else, but I changed it toCode:int binValue = binaryConv(buf[i]); int tempBinValue = binValue;
now. Thanks.Code:int tempBinValue = binaryConv(buf[i]);
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
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.
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 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; }
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
Here is a simpler version of what I'm trying to do...
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?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); }
Actual output:
Code:128 32 8 2 0 0 0
Last edited by Azmeos; 07-10-2003 at 11:17 AM.
\0
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?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.
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.
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); }
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
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
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:
do this:Code:for(int j = 0; j <= 7; j++) { binValue = (buf[i] & bit[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));
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.