answersLogoWhite

0

In C, the tilde operator (~) is the bitwise NOT operator. It returns the ones-complement of its operand. That is, the individual bits of the input are inverted in the output, such that all 0s becomes 1s and all 1s become 0s.

Note that the bitwise NOT (~) and logical NOT (!) operators are used for entirely different purposes. With logical NOT, the operator evaluates true (the all-ones bit pattern) when the operand is false (the all-zeroes bit pattern), which is exactly the same as the ones-complement used in bitwise NOT. However, if the operand represents anything other than the all-zeroes bit pattern, the output is the all-zeroes bit pattern.

We can compare the two operators by examining what happens to the bits in each operation. Let's use the value 42 (binary 00101010) as the input.

~42 -43

!42 false

Note that the binary value 11010101 represents -42 on a ones-complement system. However, most systems today use twos-complement notation for signed values, thus if we want to negate a value regardless of which notation is utilised by the system we must use the unary minus operator. On a twos-complement system, unary minus is equivalent to adding 1 to the ones-complement representation of the operand. Thus -42 is equivalent to (~42) + 1 = (~00101010) + 00000001 = 11010101 + 00000001 = 11010110 = -42.

User Avatar

Wiki User

8y ago

Still curious? Ask our experts.

Chat with our AI personalities

CoachCoach
Success isn't just about winning—it's about vision, patience, and playing the long game.
Chat with Coach
RafaRafa
There's no fun in playing it safe. Why not try something a little unhinged?
Chat with Rafa
DevinDevin
I've poured enough drinks to know that people don't always want advice—they just want to talk.
Chat with Devin

Add your answer:

Earn +20 pts
Q: What is tilde operator in c and how its work?
Write your answer...
Submit
Still have questions?
magnify glass
imp