# Thread: Bit operator in c programming

1. ## Bit operator in c programming

In the c programming, operations can be performed on a bit level using bit wise operator

Bits that are 0 become 1, and those that are 1 become 0. For example:

bit a ~a
0 1
1 0

I have used bit wise operator in program output should be 0 but it show output -2. why does it show -2 ?
Code:
```#include<stdio.h>

int main()
{

int a = 1;

return 0;
}```
Result -2 2. It shows -2 because the computer uses two's complement to represent negative numbers, and the ~ operator inverts all the bits, not just the first one.

Long story short, the negative of a binary number "n" can be represented as "~n + 1" 3. Originally Posted by GReaper It shows -2 because the computer uses two's complement to represent negative numbers, and the ~ operator inverts all the bits, not just the first one.

Long story short, the negative of a binary number "n" can be represented as "~n + 1"
Thank you for your reply. I was trying to write c program for following condition with bitwise operator

bit a ~a
0 1
1 0

How to test above condition ? 4. If you want only a single bit to be displayed, you need a bit-mask, like this:
Code:
`(~a & 1)` 5. I'm not really sure I understand what you're getting at, but if you want to make 1 into binary 10, the simplest thing to do is add.

You could also xor 3, since 1 xor 3 = 2. 6. I found the windows calculator very helpful in understanding bitwise. You can just switch it from Standard to Programmer (or use the hotkey Alt 3). It then shows your calculations in binary. It also allows you to use bitwise operators. 7. Originally Posted by abhi143 Thank you for your reply. I was trying to write c program for following condition with bitwise operator

bit a ~a
0 1
1 0

How to test above condition ?
Why do not you use unsigned rather than signed values ?

AFAIK you rarely use bit-twiddling with signed values. Popular pages Recent additions bit, int, operator, output, show 