^^ beat you too it
Are you even supposed to use shift operators on signed integers? Isn't it undefined behavior?
^^ beat you too it
Are you even supposed to use shift operators on signed integers? Isn't it undefined behavior?
Hello,
According to Experts Exchange:
"Padding when shifting to the right is different for SIGNED and UNSIGNED integers. The sign is the highest bit in a signed integer, so if you have a negative number, it will stay negative. If the number would have been unsigned, a zero would have shifted in.
It is exactly equal to a division by 2, SIGNED or UNSIGNED: the sign stays."
- Stack Overflow
Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.
Thanks for all the help!
The #define has some weird side effects without extra precautionsOriginally Posted by Stack Overflow
Output:Code:#include <stdio.h> #define isPositive(x) !!x & !((x >> 31) & 1) int main(void) { int i; for(i = -4;i < 3;++i) printf("%d - %d\n", i+1, isPositive(i+1)); return 0; }
Code:-3 - 0 -2 - 0 -1 - 0 0 - 0 1 - 1 2 - 0 3 - 0
If you understand what you're doing, you're not learning anything.
I see,
I highly recommend tihs implementation:On a side note to Brian's question: The shift and bitwise operators may only be used on integral types. Results of the shift and bitwise operators on signed integrals are not necessarily identical on all machines (those using one's complement and two's complement arithmetic will differ, for example). It is therefore recommended that these operators only be used on unsigned integers.Code:#define isPositive(x) ((x + 0x7FFFFFFF) >> 31 & 1)
- Stack Overflow
Last edited by Stack Overflow; 02-17-2005 at 04:25 PM.
Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.
One more, for fun.[edit]I believe this works on platforms that are not 32-bit as well.Code:int isPositive(int x) { return !!x & !(x & (~(~0U >> 1))); }sizeof(int) = 4
isPositive(-2147483648) = 0
isPositive(-2147483647) = 0
isPositive(-2) = 0
isPositive(-1) = 0
isPositive( 0) = 0
isPositive( 1) = 1
isPositive( 2) = 1
isPositive(2147483646) = 1
isPositive(2147483647) = 1sizeof(int) = 2
isPositive(-32768) = 0
isPositive(-32767) = 0
isPositive(-2) = 0
isPositive(-1) = 0
isPositive( 0) = 0
isPositive( 1) = 1
isPositive( 2) = 1
isPositive(32766) = 1
isPositive(32767) = 1
Last edited by Dave_Sinkula; 02-17-2005 at 04:43 PM.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
According to C89 Standard (Section 3.3.6 of the Draft Standard --- I don't have an official copy) and C99 standard (Section 6.5.7):Originally Posted by Stack Overflow
There are even footnotes using this as an example of implementation-defined behavior (identical wording in C89 and C99):The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has
an unsigned type or if E1 has a signed type and a nonnegative value,
the value of the result is the integral part of the quotient of E1
divided by the quantity, 2 raised to the power E2 . If E1 has a signed
type and a negative value, the resulting value is
implementation-defined.
I recognize that your later post recommended not depending on right-shifted results for negative ints. Just thought I would mention that Brian's objection has foundation in the Standards.An example of implementation-defined behavior is the propagation of the high-order bit when a signed integer is shifted right.
I don't subscribe to Experts Exchange, so I don't know the context of the quote that you used. If they were talking about "arithmetic right shift" instructions of machines with 2's complement integer representation, I understand. If they were talking about C, well ...
Regards,
Dave
Last edited by Dave Evans; 02-17-2005 at 05:46 PM.