Thread: bitwise operators

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    7

    bitwise operators

    Hi,

    I need to write a function that will return 1 if x is > 0 otherwise return 0 using only the following operations and no loops:
    ! ~ & ^ | + << >>


    My code works for positive and negative numbers, but does not work for 0. How can I evaluate 0 as being not greater then 0?

    Here is my code:
    Code:
    int isPositive(int x) {
    
    x = x >> 31;
    x = x & 1;
    x = !x;
    
    
      return x;
    }

    I'm probably missing something obvious, but no matter which way I try it, 0 always comes back as 1.

    Any ideas?

  2. #2
    Hello,

    This is becuase the line: x = !x changes your value before returning the answer. Let's take a look step by step as x = 0:

    "0 = 0 >> 31;" = 0
    "0 = 0 & 1;" = 0
    "0 = !0;" = 1
    "return 1;"


    - 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. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    7
    Right, 0 should be 0, not 1. But if I shift 0 to the right it is going to have the same affect as a positive number.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    It's supposed to do that, stack overflow otherwise it would return the wrong answer for any input. It only returns the wrong answer for zero.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You used the '=' operator. Doesn't that violate the conditions?
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    7
    I can use = I can't use ==

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
       return !!x & !((x >> 31) & 1);
    The !!x will be 0 for zero and 1 for any nonzero value of x.
    Last edited by Dave_Sinkula; 02-17-2005 at 04:08 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.*

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    #include <stdio.h>
    
    int ispositive(int x)
    {
      return !((x >> 31) & 1) & !!x;
    }
    
    int main(void)
    {
      int i;
    
      for(i = -3;i < 4;++i)
        printf("%d - %d\n", i, ispositive(i));
    
      return 0;
    }
    My output:
    Code:
    -3 - 0
    -2 - 0
    -1 - 0
    0 - 0
    1 - 1
    2 - 1
    3 - 1
    EDIT: Bah! Dave_Sinkula beat me to it
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Code:
    int isPositive(int x) 
    {
       return ((x + 0x7FFFFFFF) >> 31) & 1;
    }
    hehe i cheated.
    Last edited by Brian; 02-17-2005 at 04:12 PM.

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You used '=' too, Brian!
    If you understand what you're doing, you're not learning anything.

  11. #11
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    he said we're allowed.

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    So he did. I missed that post. Sorry.
    If you understand what you're doing, you're not learning anything.

  13. #13
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Quote Originally Posted by itsme86
    So he did. I missed that post. Sorry.
    there I fixed it :P

  14. #14
    Registered User
    Join Date
    Feb 2005
    Posts
    7
    = yes, == no.

    she not he

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

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