5

I have

int x = 5;
printf("%d", x); //i get 5... expected

x = !x;
printf("%d", x);// i get 0... hmm 

5 in binary is: 0101 if we apply the inverse to each bit, we should get 1010, but ! is not necessarily an inverter, it's a logical operator. Why do i get a 0 ?

is the reason that, in C, a positive number is treated as true and so !-ing it would result in 0? is this compiler specific?

fifamaniac04
  • 2,343
  • 12
  • 49
  • 72

3 Answers3

24

The not (!) operator returns either 0 or 1, depending on whether the input is non-zero or 0 respectively.

If you are looking for a bitwise negation, try ~x.

Andrey Mishchenko
  • 3,986
  • 2
  • 19
  • 37
4

! is a logical operator. !expr has value 0 if expr has value non-zero. You need bitwise ~ (NOT) operator.

x = ~x;
haccks
  • 104,019
  • 25
  • 176
  • 264
  • 1
    Why the down vote ? Comment please.... – haccks Jan 10 '14 at 23:19
  • to be honest it's because your original answer included nothing that hadn't already been on the answer page for several minutes, was harder to read, and did not give the OP what he was (obviously) looking for, which is the bit-wise negation (for the record this was edited in), so i didn't really understand what the point was of cluttering the answer list of such a simple question – Andrey Mishchenko Jan 10 '14 at 23:25
  • 1
    @Andrey, seeing the times you were only 2 minutes apart. It can easily be that haccks hadn't seen your answer before giving his. I think in such cases it would be nicer to give the person the benefit of the doubt, or at least directly leave a comment. – Jens Gustedt Jan 10 '14 at 23:28
  • @Andrey; Oh! I understand the root reason of the downvote. To be honest you downvoted after my edit. By the way, thanks for your *valuable comment*. – haccks Jan 10 '14 at 23:29
  • 1
    @JensGustedt Do you think this answer took 2 minutes to write? I would give the benefit of the doubt but haacks does this all the time lol. just silly, I really don't get it – Andrey Mishchenko Jan 10 '14 at 23:29
  • @haccks votes are a popularity contest anyway, I spent 10 seconds writing an answer here and got 6 votes in 15 minutes, spend two hours solving a difficult algorithmic question and get 10 rep or 0 in the worst case. and really I absolutely do not understand why you posted an answer here except that you were hoping to get rep – Andrey Mishchenko Jan 10 '14 at 23:31
  • 1
    @Andrey, please calm down, don't put up a fight for nothing. SO is here to help people, that should be your major concern. Pages don't always get refreshed when you would like to. And supposing that he did that just for reputation is ridiculous. He is beyond the 10k bound, has all the priviledges he can get. – Jens Gustedt Jan 10 '14 at 23:32
  • @JensGustedt; You are right. My internet connection is slow since last week. This creates some problem in posting answers. As you can see that I am rep capped now, I did't posted this answer for competing Andrey and cluttering the answer. – haccks Jan 10 '14 at 23:32
  • @JensGustedt lol I'm not the one who manually tracks individual posters' rep to see who gets that -1 post-downvote – Andrey Mishchenko Jan 10 '14 at 23:34
  • 3
    @Andrey, I don't think that its for the rep itself. If I'd take the pain to answer such a basic question (you both should have searched for duplicates first) and then someone votes me down, I would be pissed off, no question. – Jens Gustedt Jan 10 '14 at 23:37
  • @Andrey; *really I absolutely do not understand why you posted an answer here except that you were hoping to get rep*: This time you are wrong. I am re-capped now (`200` limit per day). Voting up my answer not gonna add any reputation to my poster. If I could have seen your answer then I did't posted my answer. – haccks Jan 10 '14 at 23:38
  • @Andrey; *lol I'm not the one who manually tracks individual posters' rep to see who gets that -1 post-downvote*: That was just a guess (some mathematics calculations). I lied to you. :) – haccks Jan 10 '14 at 23:44
  • 1
    @Jens: "He is beyond the 10k bound, has all the privileges he can get": true for on-site privileges, not quite true in general. Sometimes SO sends swag to the top N users by rep, so there's always something to fight for :-) – Steve Jessop Jan 10 '14 at 23:53
  • @SteveJessop; How much and how many times you get that :P – haccks Jan 10 '14 at 23:55
  • 1
    @haccks: Twice I think. I have a T-shirt and mini cornhole toss game. And some pens. And for all I know there are even better perks *right* at the top! I think what I was most impressed by is that they paid the international shipping no questions asked... – Steve Jessop Jan 10 '14 at 23:56
  • @SteveJessop, damm, I missed all of that, so I really have to improve myself a lot :) – Jens Gustedt Jan 10 '14 at 23:59
  • 1
    At least I prefered hacks answer, because of the hint that ! is a logical opertation. I just forgot this and hhacks answer was in my opion the C fundamentalistic answer I was looking for. Thx. – bernie3280109 May 02 '22 at 12:07
4

The result of expression !x has type bool (in C++) and equal to false if x is not equal to 0 and true otherwise.

In your case !x will be equal to false because x == 5. Then this value (false) is converted to type int in the assignment statement and x becomes equal to 0.

Maybe you meant the operator ~? That is

x = ~x;

I was speaking about C++. In C the value of operator is either 0 or 1 corresponding to false and true in C++ and the result has type int.

Wildfire
  • 6,358
  • 2
  • 34
  • 50
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335