views:

78

answers:

4

Reading from a pipe:

    unsigned int sample_in = 0; //4 bytes - 32bits, right?


      unsigned int len = sizeof(sample_in); // = 4 in debugger
      while (len > 0)
   {
    if (0 == ReadFile(hRead,
                          &sample_in,
                          sizeof(sample_in),
                          &bytesRead,
                          0))
    {
                    printf("ReadFile failed\n");

    }

    len-= bytesRead; //bytesRead always = 4, so far

   }

In the debugger, first iteration through:

sample_in = 536739282 //36 bits?  

How is this possible if sample in is an unsigned int? I think I'm missing something very basic, go easy on me!

Thanks

+3  A: 

536739282 is well within the maximum boundary of an unsigned 4 byte integer, which is upwards of 4 billion.

Josh Matthews
+2  A: 

536,739,282 will easily fit in an unsigned int and 32bits. The cap on an unsigned int is 4,200,000,000 or so.

DeadMG
+2  A: 

unsigned int, your 4 byte unsigned integer, allows for values from 0 to 4,294,967,295. This will easily fit your value of 536,739,282. (This would, in fact, even fit in a standard signed int.)

For details on allowable ranges, see MSDN's Data Type Ranges page for C++.

Reed Copsey
+3  A: 

Judging from your comment that says //36 bits? I suspect that you're expecting the data to be sent in a BCD-style format: In other words, where each digit is a number that takes up four bits, or two digits per byte. This way would result in wasted space however, you would use four bits, but values "10" to "15" aren't used.

In fact integers are represented in binary internally, thus allowing a 32-bit number to represent up to 2^32 different values. This comes out to 4,294,967,295 (unsigned) which happens to be rather larger than the number you saw in sample_in.

Mark B
By BCD-style you mean hexadecimal?
PeterK
@PeterK no, I mean every four bits represents one decimal digit from 0-9 with six wasted unused representations. Nine digits in OP's question * four bits/digit = the 36 bits in comment in OP.
Mark B