How are int, float, double, etc. store as Bytes?

I understand how a char is a byte, that ranges from 0 to 255 unsigned.
I know an int is 4 bytes, but how does the system really store an int? Does it store it in a size 4 byte array? If so, can someone explain how the system converts the byte array to int? I've seen this code by Duoas, but I don't really understand:
1
2
3
4
5
6
7
8
9
10
11
12
template <typename IntegerType>
IntegerType bitsToInt( IntegerType& result, const unsigned char* bits, bool little_endian = true )
  {
  result = 0;
  if (little_endian)
    for (int n = sizeof( result ); n >= 0; n--)
      result = (result << 8) +bits[ n ];
  else
    for (unsigned n = 0; n < sizeof( result ); n++)
      result = (result << 8) +bits[ n ];
  return result;
  }

what does "(result << 8)" mean? I tried it on my compiler, and it's apparently equal to result * 256. I would also like to know how the double type is converted to byte and vice-versa. Thanks!
It's a bit shift. It directly modifies the actual bits of the data. However, afaik, type storage varies by compiler and OS.
How a system stores an int depends on the system. You could do a google search on the term endianness and find a bunch of articles on that subject alone that would explain it better than I could.

result << 8 shifts the bits 8 times to the left. Bit 0 (LSB) ends up in Bit 8, bit 8 in 16, and so forth. The 8 MSBs are lost. Result is 0 to begin with so shifting is not causing anything to be lost. Some zeroes are simply falling off the left side of the integer. He is adding each byte from the array to the integer and then shifting left by 8 to move the new byte out of the way. The loop does this repeatedly so that when it is finished all bytes from the character array have been copied. You might have to draw yourself a picture but in each of the code blocks he starts from a different end of the character array. If you call the function twice once with little_endian true and once with it false you can debug and inspect the result to see what happened. Make sure you use the same array input for both calls but ensure that each byte is something different so that you can easily inspect the result and notice the difference. Or you can just print the results to the console just as easily and inspect that.
Hmm, ya, I understand now. But what about the double type? How would one go about converting a byte array to a double?
Wikipedia provides a really good article about how double looks like in memory:

http://en.wikipedia.org/wiki/Double_precision_floating-point_format

In seminars about mathematical computation or similar you learn more about datatypes and so on.
So i can recommend you to visit university and just sit into some lectures or so. Nobody will bother with it.
Last edited on
Are you getting data over a bus and know that it is a double and need to convert an array of bytes to a double? What do you want to do?
Thanks maikel, the wikipedia link helped!

Btw, I am already a college student (University of Michigan) :). But in the class I'm taking, we are just doing algorithms and data structures. I'm also reluctant to speak to the professor.

I'm learning about the ReadProcessMemory() and WriteProcessMemory() functions, and I have done some basic tasks (i.e. change the solitaire score, mess with calculator.exe), so that's why I want to learn about byte arrays. I'm not interested in hacking games and stuff, but I just find these functions very interesting, and they also further my knowledge of computers in general.
Topic archived. No new replies allowed.