If one writes in C or C++
Is it guaranteed regardless of platform that x is now 4? I was thinking it would have to be, but a friend was claiming that on some platforms the value may be different.Code:int x = 1 << 2;
If one writes in C or C++
Is it guaranteed regardless of platform that x is now 4? I was thinking it would have to be, but a friend was claiming that on some platforms the value may be different.Code:int x = 1 << 2;
Yes. Here's the relevant text from the next most recent version of the C standard, where ** denotes exponentiation:Originally Posted by Aisthesis
Originally Posted by C99 Clause 6.5.7 Paragraph 4
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
The answer lies in the C standard:
Basically, in that case, it will always be 4, regardless of platform, since E1 (1 in your expression) is positive, and E1 * 2^E2 fits in an integer. If you used -2 instead of 2, or E1 was really large (like 2,147,483,647), behavior would be undefined, i.e. you can't trust it on any platform. It's the right-shift operator that is has implementation-defined behavior and may vary from platform to platform.Originally Posted by C99 6.5.7
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Hmm! so what's your point, because that'd be every piece of high level language code
Not sure what you are trying to get at here? How is that going to upset calculations or computations that will be performed? Unless you provide more details I'm unable to see the light of your argument.
Reading it again I still think it's perfectly clear. Perhaps you understand code better:If you could point out the spot where the data was moved between systems with different endianness in the above code, I'd be happy to take my comment back.Code:int x = 0x12345678; char *y = (char*)&x; printf("%c", y[1]);
If on the other hand you can see now how the above code will simply give different results for the different systems it may be run on then perhaps you understand my comment now.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
In that case you missed the point I was making in my post i.e. endianness doesn't affect computations or comparisons done on disparate systems.
Will give the same output irrespective of endianness, otherwise what's the point.Code:int i, j, k; k = i * j;
I was referring to the OP's concern on bit shifting an int quantity, whose output doesn't depend on endianness. Of course walking along an int or float with a char* would print the bytes from the left on big endian and from the right on little endian but that is not a computation or a comparison but merely a way of showing how data is stored in memory on systems with different endianness.
Hope that makes it clear
Alright, we understand each other. I was clearly a bit nitpicky on your use of the word "only".
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"