Thread: Logical negation

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    27

    Logical negation

    does anyone know of another way to write !(x)
    x being a 32 bit int.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    88
    x==0

    or

    x?0:1

    No - there is no good way... Why do you ask?
    Hope you don't mind my bad english, I'm Austrian!

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    If you are using making a 32-bit boolean you could always do something like:

    ~x

    This would flip all the bits. Likewise--and still using the boolean assumption--you could do this:

    x^1

    I think this is the least expensive means of accomplishing the task, but I don't know so don't quote me on that.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by master5001
    If you are using making a 32-bit boolean you could always do something like:

    ~x

    This would flip all the bits. Likewise--and still using the boolean assumption--you could do this:

    x^1

    I think this is the least expensive means of accomplishing the task, but I don't know so don't quote me on that.
    The first example would only reverse the bits. Thus, unless the number was a full set of 'on' bits (ie: the maximum value of the unsigned variable, or the minimum of an unsigned), then flipping all the bits would still product a "not equal to zero" value.

    Remember, !something means that unless 'something' is zero, the ! value will be zero. Oddly enough the opposite is true. !0 = true, !!0 = not true.

    x^1 would only toggle ones value. I suppose that would technicly work, since any modification to any bit would then "not be x".

    You could always do:

    x != x

    That's another way of writing !x. Since x does in fact equal x, the test of x != x would fail, and produce the same answer (truth test) as !x.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: Logical negation

    Originally posted by samps005
    does anyone know of another way to write !(x)
    x being a 32 bit int.
    !x produces a logical negation of x. period. the end. no reason to do it any other way. and any other way is going to most certainly be longer and harder to read.
    hello, internet!

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You could always do:

    x != x

    That's another way of writing !x. Since x does in fact equal x, the test of x != x would fail, and produce the same answer (truth test) as !x.
    Sorry, but:
    Code:
    x       !x      x != x
    иииииииииииииииииииииииииииииииии
    true    false   false
    false   true    false             <-- Doesn't equal
    x is always x, so x != x is always false, but one of x and !x is always true.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >does anyone know of another way to write !(x)
    Sure:
    Code:
    #include <stdio.h>
    
    int logical_not ( int val )
    {
      if ( val != 0 )
        return 0;
      else
        return 1;
    }
    
    int main ( void )
    {
      int w = 5;
      int x = 0;
      int y = 1;
      int z = -5;
    
      printf ( "%d\n", !w );
      printf ( "%d\n\n", logical_not ( w ) );
    
      printf ( "%d\n", !x );
      printf ( "%d\n\n", logical_not ( x ) );
    
      printf ( "%d\n", !y );
      printf ( "%d\n\n", logical_not ( y ) );
    
      printf ( "%d\n", !z );
      printf ( "%d\n\n", logical_not ( z ) );
    
      printf ( "%d\n", !!w );
      printf ( "%d\n\n", logical_not ( logical_not ( w ) ) );
    
      printf ( "%d\n", !!x );
      printf ( "%d\n\n", logical_not ( logical_not ( x ) ) );
    
      printf ( "%d\n", !!y );
      printf ( "%d\n\n", logical_not ( logical_not ( y ) ) );
    
      printf ( "%d\n", !!z );
      printf ( "%d\n\n", logical_not ( logical_not ( z ) ) );
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Magos
    Sorry, but:
    Code:
    x       !x      x != x
    иииииииииииииииииииииииииииииииии
    true    false   false
    false   true    false             <-- Doesn't equal
    x is always x, so x != x is always false, but one of x and !x is always true.
    Note to self: Don't post code when you've had one too many passes of the peace pipe.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical And
    By shiju in forum C Programming
    Replies: 6
    Last Post: 01-11-2004, 11:15 AM
  2. bitwise negation
    By Sathyabodh in forum C Programming
    Replies: 1
    Last Post: 08-12-2003, 09:42 AM
  3. Logical Error
    By gardenair in forum C Programming
    Replies: 2
    Last Post: 04-06-2003, 04:18 PM
  4. Size of 1 pixel
    By ooosawaddee3 in forum C++ Programming
    Replies: 4
    Last Post: 07-26-2002, 08:06 PM