I have a small problem understanding a function as to what its doing:
I have run this program in C++. I will comment the lines of code as per my understanding. Your insight would be useful
unsigned int myfunc(unsigned int n)
{
unsigned int temp =
(n >> 24) | //logical shift done here as it is unsigned, division
((n << 8) & 0x00FF0000) | // logical shift as well, multiplication
((n >> 8) & 0x0000FF00) |
(n << 24);
return temp;
}
As an example we input an unsigned integer for example 55 the output is 922746880. I am told this is an application that is used in real world scenarios. I am a beginner C++ newbie.
That code reverses the byte order of n. It is used for transforming values between big-endian and little-endian byte orders. See http://en.wikipedia.org/wiki/Endianness
Network Byte Order (big-endian) is the order the internet and most other peer-to-peer software uses, but Intel PCs use little-endian order. These two are the most common, but there are other systems out there that were designed by brain-damaged folk.
unsignedint myfunc(unsignedint n)
{
unsignedint temp =
(n >> 24) | //logical shift done here as it is unsigned, division
((n << 8) & 0x00FF0000) | // logical shift as well, multiplication
((n >> 8) & 0x0000FF00) |
(n << 24);
return temp;
}
1 2 3 4 5 6 7 8 9
unsignedint myfunc(unsignedint n)
{
unsignedint temp =
(n >> 24) | //logical shift done here as it is unsigned, division by 2 ** 24, bitwise or afterwards.
((n << 8) & 0x00FF0000) | // logical shift as well and reading the 3th and 4th byte of it, bitwise or
((n >> 8) & 0x0000FF00) | //pretty similair to above but reading 5th and 6th byte
(n << 24); //Shifting 24 bytes to the left.
return temp;
}
Is this some hash function, rng, or does it something mathematical?
I know nothing about it, but it seems to me that mixing byte order and component order is still pretty brain-damaged. Why not store things in the same order as the processor?
[edit] @Somelauw Get out a piece of paper and draw yourself what it does.
Duoas: Your not referring to me? Endianness doesn't affect Fixed Point Numbers, but I find it brain damaged thinking :P IEEE754 for example. Gah.. It just makes crap difficult.
Ladsidude/Somelauw: I will simplify that code for you.
Typically, an unsigned int is 4 bytes (0x11223344). Each Byte is represented by 2 hex chars (0x12). You code just swaps them from 0x11223344 to 0x44332211.
1 2 3 4
(n >> 24) | // Move first byte to the end
((n << 8) & 0x00FF0000) | // move 2nd byte to 3rd
((n >> 8) & 0x0000FF00) | // move 3rd byte to 2nd
(n << 24); // move last byte to start
The & is used to turn bits on/off (simple explanation). So by going X & 0x00FF0000 they are turning all the bits off for bytes 1, 3 and 4. But leaving all (0xFF = All) bits for byte 2 on.
IEEE754 is the weirdest standard i've worked with. Typically when working with bits you start at the right (lowest value) and work left.
So you'd have
1 2 4 8 etc. Pretty common Binary math,
IEEE754 does this for the EXP part of the value, but the mantisse goes from left to right doing 1/N each time. This is retarded because, in theory you should be able to go from right->left like this with no problem. But CPU architecture being not as accurate as it could be gives you subtle rounding differences at the lower bits (e.g. 1 2 5 9 18 37) so your value is always going to be wrong =\ Brain Damaged imo
Thank you very much for the reply. I feel this is a simple explanation. Perhaps as time passes and I get more familiar with C and computer architecture, it would be helpful to me.