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, constunsignedchar* bits, bool little_endian = true )
{
result = 0;
if (little_endian)
for (int n = sizeof( result ); n >= 0; n--)
result = (result << 8) +bits[ n ];
elsefor (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!
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.
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.
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.