## mingw-users

 [Mingw-users] bit operators From: Henning, Brian - 2002-09-12 14:15:09 ```Hello- i am trying to read a binary file, and i am running into a problem getting some info out of the file. I have two bytes next to each other. what i want is a combo of the first byte and half of the second byte and put it into an int variable. can i use the shift operators to do this? can someone show me an example? thanks, brian ```
 [Mingw-users] Re: bit operators From: Dan Muller - 2002-09-12 15:10:47 ```"Henning, Brian" wrote in message news:E1846117A30764468D2192D5A48541CC03894E6B@... > Hello- > i am trying to read a binary file, and i am running into a problem getting > some info out of the file. I have two bytes next to each other. what i want > is a combo of the first byte and half of the second byte and put it into an > int variable. can i use the shift operators to do this? can someone show me > an example? > thanks, > brian The problem is under-specified. Is the upper nibble of the second byte supposed to form the upper or lower part of the final result? (Nibble = half a byte; first time I've gotten to use that term in years.) Do you want the upper or lower nibble of the second byte? Is the result supposed to be a signed quantity, or are we assuming the result is a positive number, with its upper nibble zero? Assuming the result should be an integer with a zero upper nibble, then the first byte, and then the upper nibble of the second byte in the least-significant nibble of the result, you might do it like this: unsigned char byte1, byte2; .... int result = byte1 * 16 + (byte2 & 0xf0) / 16; You could use shifts, but I think it's unspecified as to whether a right-shift replicates the most significant bit or not, so you have to gaurd against this: int result = (byte1 << 4) | ((byte2 >> 4) & 0x0f); Here's a small C++ example that verifies both expressions: #include int main() { const unsigned char byte1 = 0xed; const unsigned char byte2 = 0xcb; std::cout << std::hex << (byte1 * 16 + (byte2 & 0xf0) / 16) << std::endl; std::cout << std::hex << ((byte1 << 4) | ((byte2 >> 4) & 0x0f)) << std::endl; } ```
 Re: [Mingw-users] bit operators From: Marc Kealy - 2002-09-12 15:18:15 ```----- Original Message ----- From: "Henning, Brian" To: " (E-mail)" Sent: Thursday, September 12, 2002 3:13 PM Subject: [Mingw-users] bit operators > Hello- > i am trying to read a binary file, and i am running into a problem getting > some info out of the file. I have two bytes next to each other. what i want > is a combo of the first byte and half of the second byte and put it into an > int variable. can i use the shift operators to do this? can someone show me > an example? > thanks, > brian > If the value was written in the normal fassion, then the lo-order byte is first. So:- char c; // read lo-order byte ifs >> c; // reasign int iResult =c; // read hi-order byte ifs >> c; // if you want the hi-order nibble c &= 0xf0; // or if you want the lo-order nibble c &= 0x0f; // combine the two while shifting the hi-order byte iResult |= c << 8; I hope this works for you. -- Marc ```
 Re: [Mingw-users] bit operators From: Marc Kealy - 2002-09-12 16:36:09 ```> From: "Henning, Brian" > > > Hello- > > i am trying to read a binary file, and i am running into a problem getting > > some info out of the file. I have two bytes next to each other...[snip...] From: "Marc Kealy" > If the value was written in the normal fassion, then the lo-order byte is > first. So:- > > char c; STOP! Sorry about that. Should read:- unsigned char c; > > // read lo-order byte > ifs >> c; [snip...] -- Marc ```