Bitwise NOT operator returning unexpected and negative value? [duplicate]

 ̄綄美尐妖づ 提交于 2019-12-20 07:09:18

问题


I'm trying to get the value of an integer using Bitwise NOT, but i'm not getting what i expected.

#include <stdio.h>

int main(){
    int i = 16;
    int j = ~i;
    printf("%d", j);
    return 0;
}

Isn't 16 supposed to be:

00000000000000000000000000010000

So ~16 is supposed to be:

11111111111111111111111111101111

Why i'm not getting what i expected and why the result is negative?

This is what i'm trying to do:

I have a number for exemple 27 which is:

00000000000000000000000000011011

And want to check every bit if it's 1 or 0.

So i need to get for exemple this value

11111111111111111111111111110111

The use second one to check if the 3rd bit of the first is set to 1.


回答1:


And want to check every bit if it's 1 or 0.

To check an individual bit, you don't NOT the number, you AND it with an appropriate bit mask:

if ((x & 1) != 0) ... // bit 0 is 1
if ((x & 2) != 0) ... // bit 1 is 1
if ((x & 4) != 0) ... // bit 2 is 1
if ((x & 8) != 0) ... // bit 3 is 1
...
if ((x & (1 << n)) != 0) ... // bit n is 1
...
if ((x & 0x80000000) != 0) ... // bit 31 is 1



回答2:


Although there are pedantic points which can be made about compiler behaviour, the simple answer is that a signed int with the top bit set is a negative number.

So if you do something which sets the top bit of an int (a signed int, not an unsigned one), then ask the tools/library to show you the value of that int, you'll see a negative number.

This is not a universal truth, but it's a good approximation to it for most modern systems.

Note that it's printf which is making the representation here - because %d formats numbers as signed. %u may give the result you're expecting. Just changing the types of the variables won't be enough, because printf doesn't know anything about the types of its arguments.

I would say that as a general rule of thumb, if you're doing bit-twiddling, then use unsigned ints and display them in hexadecimal. Life will be simpler that way, and it most generally fits with the intent. (Fancy accelerated maths tricks are an obvious exception)




回答3:


In two-complement arithmetic to get a revers number (for exam[ple for value 16 to get value -16) you need reverse each bit and add 1.

in you example to get - 16 from 16 that is represented as

00000000000000000000000000010000

you nedd reverse each beit. You will get

11111111111111111111111111101111

now you must add 1 and you will get

11111111111111111111111111110000

As you can see if t add these two value you will get 0. It proves that you did all correctly.




回答4:


If you want to get ones' complement of a number, you need to put that number into an unsigned variable and show it as so. In C it would be:

unsigned int x = ~16;
printf("%u\n", x);

and you will get 4294967279. But if you are just trying to get the negative number of a certain one, put the - operator before it.

EDIT: To check whether a bit is 0 or 1, you have to use the bitwise AND.



来源:https://stackoverflow.com/questions/20728828/bitwise-not-operator-returning-unexpected-and-negative-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!