Thread: bitwise operators

  1. #16
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    ^^ beat you too it

    Are you even supposed to use shift operators on signed integers? Isn't it undefined behavior?

  2. #17
    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.

  3. #18
    Registered User
    Join Date
    Feb 2005
    Posts
    7
    Thanks for all the help!

  4. #19
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Stack Overflow
    Heh,

    How about these:
    Code:
    #define isPositive(x) ((x + 0x7FFFFFFF) >> 31 & 1)
    Code:
    #define isPositive(x) !!x & !((x >> 31) & 1)
    Made to make itsme86 happy.


    - Stack Overflow
    The #define has some weird side effects without extra precautions
    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;
    }
    Output:
    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.

  5. #20
    I see,

    I highly recommend tihs implementation:
    Code:
    #define isPositive(x) ((x + 0x7FFFFFFF) >> 31 & 1)
    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.


    - 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.

  6. #21
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    One more, for fun.
    Code:
    int isPositive(int x)
    {
       return !!x & !(x & (~(~0U >> 1)));
    }
    [edit]I believe this works on platforms that are not 32-bit as well.
    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) = 1
    sizeof(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.*

  7. #22
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Stack Overflow
    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
    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):

    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.
    There are even footnotes using this as an example of implementation-defined behavior (identical wording in C89 and C99):

    An example of implementation-defined behavior is the propagation of the high-order bit when a signed integer is shifted right.
    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.

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise operators
    By gnewfenix in forum C Programming
    Replies: 2
    Last Post: 05-16-2009, 08:43 PM
  2. Bitwise Operators
    By rrc55 in forum C Programming
    Replies: 6
    Last Post: 04-30-2009, 11:37 AM
  3. Palindromes and Bitwise operators
    By Dr Tornillo in forum C Programming
    Replies: 8
    Last Post: 08-02-2007, 02:31 PM
  4. bitwise and arithmetic Operators
    By Whiteghost in forum C Programming
    Replies: 4
    Last Post: 12-28-2006, 02:13 PM
  5. Bitwise Operators, Help!!
    By Mini__C in forum C Programming
    Replies: 6
    Last Post: 07-14-2004, 04:20 PM