Bitwise One’s Complement Operator in C ( ~ ) with ex Programs
Introduction:
In our previous tutorials, We have looked at the Bitwise Right shift and Left shift operators. In today’s article, We are going to look at the Bitwise One’s Complement Operator in C programming language with example programs.
📢 This Article is part of the Bitwise Operators in C language series.
Bitwise One’s Complement Operator in C ( ~ ):
The bitwise one’s complement operator converts all the zero(0) bits to One(1) and All One(1) bits to Zero(0). So it Inverts all bits in the binary sequence.
The one’s complement operator works on single operands. So the Bitwise One’s complement operator is the Unary Operator.
The other bitwise operators like Bitwise OR, AND, XOR, Right shift, and Left-shift operators are Binary operators, which need two operands.
The One’s complement operator is denoted with ~ (Tilde).
📢 Bitwise One’s complements operator (~) Inverts all bits in the number (binary sequence).
Example to understand the Bitwise One’s complement Operator in C:
Let’s look at a couple of examples to make our understanding concrete.
Example 1:
Let’s say X = 18;
Let’s perform the one’s complement operation on variable ‘X’
~X = ~(18)
As we already know the bitwise operators work on the bit level, We need to convert the decimal number 18 to a binary sequence.
~ (18) = ~ (00010010)2
The bitwise one’s complement simply inverts all bits in the binary sequence, So we need to change all bits with zero(0)’s to One and vice versa.
~X = ~ (18) = (11101101)2
Here is the graphical representation of the above one’s complement operation.
Why the above result is negative 19 (~19)?, This is because we are using the signed Integers. We have explained it in detail in the following sections.
Example 2:
Let’s say Y = 7;
~ Y = ~ (7)
Convert 7 into a binary sequence
~ (7) = ~ (00000111)2
Let’s perform the bitwise one’s complement operation.
~ (Y) = ~ (7) = (11111000)2
We have inverted all bits in the number 7.
Here is the graphical representation of the above one’s complement operation.
📢 To make our calculations easy, We took only 8 bits to represent the integer number. Please note, The integer(int) will have 4 bytes or 32 Bits (Depending upon your compiler)
– Check Size and Limits of All Datatypes in C for more details.
Program for Bitwise one’s complement operator in C Language:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
/* Program to understand Bitwise One's Complement in C */ #include<stdio.h> int main() { printf("Bitwise One's Complement of 14 (~14) : %d\n", ~14); printf("Bitwise One's Complement of 10 (~10) : %d\n", ~10); printf("Bitwise One's Complement of 10 (~10) : %d\n", ~26); return 0; } |
Program Output:
Compile and Run the above program.
1 2 3 4 5 6 |
$ gcc ones-complement.c $ ./a.out Bitwise One's Complement of 14 (~14) : -15 Bitwise One's Complement of 10 (~10) : -11 Bitwise One's Complement of 10 (~10) : -27 $ |
Bitwise One’s complement program explanation:
If you see the above output, We got negative numbers. Why?
Example 1: ~(14):
because we used the signed integers.
Here is the step-by-step calculation.
The decimal 14 equivalent binary value : (0000000000001110)2
One’s complement of 14 is.
~ (14) = ~ (0000000000001110)2
which is equal to
(1111111111110001)2
As we are using signed integers, The above binary sequence is equal to -15 signed integer.
In the C Language, We store the negative numbers in 2’s complement form.
📢 If your requirement is mainly with the unsigned values, Then it is a good idea to use the unsigned integers for our variables.
Similarly, We got negative numbers for other values (~10, ~26) as well.
Conclusion:
We have looked at the bitwise ones complement operator in C language and understood how it works and we also went through a couple of example walk-throughs and a Program.
2 Responses
[…] Bitwise one’s complement ( ~ ) […]
[…] Bitwise Ones Complement Operator ( ~ ) […]