0

What does the !(NOT) operator do in c

decimal 6=binary 110. So, !(6) =1. But when I run the following c program i get no output if(!(6)) printf("1"); What do ! operator do in c? Why is !(6) notEqualTo 1

8th Jul 2018, 10:28 AM
Donkey
Donkey - avatar
3 Antworten
+ 5
You're talking about this I think https://www.sololearn.com/learn/4076/?ref=app
8th Jul 2018, 10:43 AM
Mert Yazıcı
Mert Yazıcı - avatar
+ 2
Yes, I thought so. For C++ using <bitset> helps in such a case. https://code.sololearn.com/cAqz7pLLT8Nu/?ref=app
8th Jul 2018, 12:24 PM
Matthias
Matthias - avatar
+ 1
As Mert Yazıcı already mentioned, you are confusing bitwise complement and not operator. printf("%d\n",~6); \\ prints -7 printf("%d\n",!6); \\ prints 0 Why is that? The decimal number is represented by more bits than only three bits. The minus comes to play because the first bit (for signed ints) represents the sign. For any *logical* operation, it is either 1 or 0. Any number different from 0 is considered as 1 (=true) So if you say !6 it becomes 0.
8th Jul 2018, 11:55 AM
Matthias
Matthias - avatar