Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does logical negation work in C?

Tags:

c

I have been using ! (logical negation) in C and in other languages, I am curious does anyone know how to make your own ! function? or have a creative way of making one?

like image 708
Ben Fossen Avatar asked Feb 23 '10 16:02

Ben Fossen


People also ask

How do you use logical negation?

The logical negation symbol is used in Boolean algebra to indicate that the truth value of the statement that follows is reversed. The symbol (¬) resembles a dash with a tail or the upper half of a rectangle. The arithmetic subtraction symbol (−) and tilde (~) are also used to indicate logical negation.

How does negation operator work?

The ~ (bitwise negation) operator yields the bitwise complement of the operand. In the binary representation of the result, every bit has the opposite value of the same bit in the binary representation of the operand. The operand must have an integral type.

How does logical operators work in C?

The logical-AND operator produces the value 1 if both operands have nonzero values. If either operand is equal to 0, the result is 0. If the first operand of a logical-AND operation is equal to 0, the second operand isn't evaluated. The logical-OR operator performs an inclusive-OR operation on its operands.

How does logical not work in C?

operator in C. Logical NOT is denoted by exclamatory characters (!), it is used to check the opposite result of any given test condition. If any condition's result is non-zero (true), it returns 0 (false) and if any condition's result is 0(false) it returns 1 (true).


1 Answers

int my_negate(int x)
{
    return x == 0 ? 1 : 0;
}
like image 141
Hans W Avatar answered Oct 16 '22 07:10

Hans W