### 24bit unsigned integer

If I want to convert 3 bytes to a '24bit unsigned integer' Is this how I would do it ?
 ``12345678910`` `````` #include using namespace std; int main() { int id3 = 11000111+(1001101<<8)+(11001010<<16); }``````

And then I need to Display the Results.!
11000111 is a decimal number, but it looks like you intended it to be a binary value. It's probably easier to handle if you use hex rather than binary. Or use a bitset.
 ``12345678910111213141516171819202122232425262728293031`` ``````#include #include #include #include using namespace std; int main() { // construct integer from three 8-bit values int id3 = 0xC7 + (0x4D<<8) + (0xCA<<16); // display result in hex and decimal cout << "id3 = " << hex << uppercase << id3 << " (hex) " << dec << id3 << " (decimal) " << endl; // construct bitset from string bitset<24> bitsa (string("110010100100110111000111")); // convert it to unsigned long and output result unsigned long a2 = bitsa.to_ulong(); cout << "a2 = " << a2; //construct another bitset from unsigned long bitset<24> bitsb(a2); // convert to string and output bits cout << " a2 (binary) = " << bitsb.to_string() << endl; return 0; }``````

 ```id3 = CA4DC7 (hex) 13258183 (decimal) a2 = 13258183 a2 (binary) = 110010100100110111000111 ```
Last edited on
Thankyou Chervil..
I was doing a Challenge. I had to Convert (199), (77) and (202). Into a byte each(199) Must go first.
Then from 'binary' to 'Hex' and combine the 'hex' together to get a 'decimal'.
I was trying to write a small code to do it.

Ok , So the '0x' tells it that i want to read a 'hex' value.!
Well, I only used hex as it was the easiest way to deal with the pseudo-binary values originally presented. You could just use the decimal values 199, 77 and 202 directly if that was the actual data given in the question.
` int id3 = 199 + (77<<8) + (202<<16);`
There may be more to this question than this, I don't think the full details of the original challenge have been stated clearly.

A bit of googling has turned up this, which I think might be the actual question:
 Let us take the following three decimal numbers: 199, 77, 202 Convert each one into a byte. (Even though 77 does not require all 8 bits to express itself, when dealing with a group of data, we usually keep it in a consistent form.) Now, take those three bytes and combine them to form a 24-bit unsigned integer. The 199 byte is the high byte (most significant) and so forth. Please enter that 24-bit integer in decimal form, and that is your answer. (Hint: your answer will not be '19977202'!)

If that is the actual question, then the solutions I gave so far have been wrong, for two reasons. Firstly, it used integer rather than byte values, and secondly the byte order in the result was incorrect.

For a byte value one might use an `unsigned char` or `uint8_t` type. Similarly the result might be an `unsigned int` or `uint32_t` type.
Include header `<cstdint>` for the latter types, which are guaranteed to occupy the specified number of bits.
Topic archived. No new replies allowed.