Bitwise One’s Complement Operator in C ( ~ ) with ex Programs

Spread the love
bitwise-ones-complement-in-c-programming-language

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.

how-bitwise-ones-complement-operator-in-c-language-works

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.

bitwise-ones-complement-in-c-explanation

πŸ“’ 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:

Program Output:

Compile and Run the above program.

ones-complement-twos-complement-program-output

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.

Related Articles:

C Programs Index:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] Bitwise one’s complement ( ~ ) […]

  2. […] Bitwise Ones Complement Operator ( ~ ) […]

Leave a Reply