c - Compiler discrepancy in expression evaluation -
I have implemented the following function for impressions if-then-other:
Int foo (int x, int y, int z) {int negOne = (1 <31) & gt; & Gt; 31; Int test =! (~ X +! X) + negOne; Int ans = (test and y). (~ Test & amp; z); Answer to return; } I have some restrictions, and the top part was a bit of hack, but it works. The test evaluates for only 0 or -1.
The assignment uses a specific compiler, and in the case of X = 0, y = -2147483648 and z = 2147483647, the compiler says that returning my code -2147483648
This will not happen because if x = 0, then test = 0. If test = 0, then the answer will be evaluation expression z, i.e., 2147483647.
I have double-checked my output on two different compilers and it says that I am returning the correct answer of 2147483647, so I think that this is a compiler error, maybe an integer Related to the limit? Of course, the mistake is in my code.
Additionally, additional compiler info: compiler is called DLC compiler. Before me, there was "parse error" problems declaring my variable in the middle of my function, and I was told that the compiler's probability was C89. Moving forward these announcements on the top of the proceedings, the problem was fixed.
UPDATE: Changing Negon expression to ~ 1 + 1 did not fix anything. I evaluated both sides of the answer expressions and, as expected, they evaluated 0 and 2147483647 respectively, So that no problem appears, Express will ultimately evaluate. 2147483647, which is 2147483647; And that is what is the value of ans, and I also checked the value of the variable to get the value of the function, and then, this is 2147483647.
Why am I still worried about why the special codear return- 2147483648
The code may display undetermined behavior if int is 32-bits The machine on which you are trying it. Left shift from Standard C 99 § 6.5.7 (Thrust Khan): Results of E1 & lt; & Lt; E2 E1 is the left-shifted E2 bit position; Empty bits are filled with zero if E1 has an unsigned type, then the value of the result is E1Ã ?? 2 E2 , at least the modules exceed the maximum value available in the result type. If there is a signed type and non-negative value in E1, and E1A ?? 2 E2 is the representable value in the result type, it is the resultant value; Otherwise, the behavior is undefined. The literal 1 is a signed int i.e. an integer signed here and has a non-negative value < Code> 1 . 1 a 2 31 = 2,147,483,648 which is not present with the 32-bit integer on the machine, because on this machine the code is signed by signed int category -2,147,483,648 to 2,147,483,647. When you are in UB's land all the bets are closed, thus any output is possible. I do not understand why you can not do this:
int negOne = -1;
Comments
Post a Comment