does anyone know of another way to write !(x)
x being a 32 bit int.
does anyone know of another way to write !(x)
x being a 32 bit int.
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!
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.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.
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.
!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.Originally posted by samps005
does anyone know of another way to write !(x)
x being a 32 bit int.
hello, internet!
Sorry, but: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.
x is always x, so x != x is always false, but one of x and !x is always true.Code:x !x x != x иииииииииииииииииииииииииииииииии true false false false true false <-- Doesn't equal
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.
>does anyone know of another way to write !(x)
Sure:
-PreludeCode:#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; }
My best code is written with the delete key.
Note to self: Don't post code when you've had one too many passes of the peace pipe.Originally posted by Magos
Sorry, but:
x is always x, so x != x is always false, but one of x and !x is always true.Code:x !x x != x иииииииииииииииииииииииииииииииии true false false false true false <-- Doesn't equal
Quzah.
Hope is the first step on the road to disappointment.