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);
This is a discussion on Bitshift with different endianess within the C Programming forums, part of the General Programming Boards category; Hello! Consider this code: Code: unsigned int i = 1; i <<= 8; printf("%d\n", i); If the processor operates with ...
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( ~_')
Yes (unless I mixed up which endianess is what).
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
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:int main(void){srand(time(0));for(double l=rand(),l0=0,l00=0;;l0+=0.1){for(double l000=0;l000 <1;l000+=.001,l+=((double)rand()/RAND_MAX)/0x64,l00+=((sin(l*0x8*atan(l0)*l000-(l0*0x8*atan (l)))*0.5)+0.5)){l00-=floor(l00);for(size_t l0000=0,l00000=(size_t)(0x50*(l00));l0000<l00000;++l0000 )putchar(0x20);putchar(0x61+(int)((double)rand()/RAND_MAX*0x1a));putchar('\n');}}return 0;}