Hello!
Consider this code:
If the processor operates with little endianess, the code will print 256. But what if it has big endianess, will it print 0?Code:unsigned int i = 1; i <<= 8; printf("%d\n", i);
Hello!
Consider this code:
If the processor operates with little endianess, the code will print 256. But what if it has big endianess, will it print 0?Code:unsigned int i = 1; i <<= 8; printf("%d\n", i);
Come on, you can do it! b( ~_')
1 << 8 will be 256 in all systems.
The byte-order (endianness) is only relevant when the processor stores the data in memory. The internal structure of a CPU register has all bits in a logical order from 0..n, in a logical right to left order. This register may be stored as 4 bytes in either big or little endian order - but shifting it 8 bits will still shuffle the entire number 8 bits to the left (or right), because this happens internally in the CPU, not on the memory outside the CPU.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Ok, but this example:
will print 00010203 if the processor uses little endianess, but 03020100 if the processor uses big endianess? Or have I got it wrong?Code:char a[4]; a[0] = 0; a[1] = 1; a[2] = 2; a[3] = 3; printf("%.8X", *(int*)a);
Come on, you can do it! b( ~_')
In fact, it's the inverse, if the processor is little endian, it will print 03020100, and if it's big endian, it will print 00010203.Originally Posted by TriKri
Of course, we are also assuming that the processor is adressing bytes, and not something more "exotic", e.g. 16-bits words.
I hate real numbers.
in other words, as long as you don't reinterpret the bit pattern as some other type then whatever manipulations/transformations you perform will be portable. otherwise, you are treading the waters of undefined behavior.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }