Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to use bitwise operator

+4 votes
asked Oct 9, 2021 by Ananya Sharma (160 points)

3 Answers

0 votes
answered Oct 9, 2021 by Peter Minarik (86,040 points)

What language do you use?

Check this for C.

0 votes
answered Oct 16, 2021 by Utpal Utkarsh (140 points)
edited Oct 17, 2021 by Utpal Utkarsh

Hello Ananya, Bitwise operators are those operator which operates on Bits of one or multiple operands.

There are six different types of Bitwise Operators in C. These are:

The Bitwise AND (&) in C: The C compiler recognizes the Bitwise AND with & operator. It takes two operands and performs the AND operation for every bit of the two operand numbers. It is a binary operator. The output of this operator will result in 1 only if both bits are 1.

The Bitwise OR (|) in C: The C compiler recognizes the Bitwise OR with | operator. It takes two operands and performs the OR operation for every bit of the two operand numbers. It is also a binary operator. The output of this operator will result in 1 if any one of the two bits is 1.

The Bitwise XOR (^) in C: The C compiler recognizes the Bitwise XOR with ^ operator. It takes two operands and performs the XOR operation for every bit of the two operand numbers. It is also a binary operator. The output of this operator will result in 1 if both the bits have different values.

Binary One’s Complement or Bitwise NOT operator (~) in C: The C compiler recognizes the Bitwise NOT with ~ operator. It takes only one operand and performs the inversion of all digits of it. It is a unary operator. The output of this operator will invert all the existing bits of that operand.

Bitwise Left shift operator (<<) in C: The C compiler recognizes the left shift operation with this <<. It takes only two operands and shifts all the bits of the first operand to the left. The second operand decides how many numbers of places this operator will shift its bits. It is a binary operator.

Bitwise Right shift operator (>>) in C: The C compiler recognizes the left shift operation with this >>. It takes only two operands and shifts all the bits of the first operand to the right. The second operand decides how many numbers of places this operator will shift its bits. It is a binary operator.

Program for Bitwise Operator in C

Let us now take a look at the program using all the bitwise operators.

#include <stdio.h>

int main()

 {

unsigned char x = 20, y = 21; // x = 20 (00010100), y = 21 (00010101)

int g = 0;

   g = x & y; /* 20 = 010100 */

   printf("The result of Bitwise AND is %d \n", g );

   g = x | y; /* 21 = 010101 */

   printf("The result of Bitwise OR is %d \n", g );

   g = x ^ y; /* 1 = 0001 */

   printf("The result of Bitwise XOR is %d \n", g );

   g = ~x;

   printf("The result of Bitwise NOT is %d \n", g );

   g = x << 1;

   printf(" The result of Bitwise Left Shift is %d \n", g );

   g = x >> 1;

   printf("The result of Bitwise Right Shift is %d \n", g );

return 0;

}

OUTPUT:

The result of Bitwise AND is 20
The result of Bitwise OR is 21
The result of Bitwise XOR is 1
The result of Bitwise NOT is -21
 The result of Bitwise Left Shift is 40
The result of Bitwise Right Shift is 10

commented Oct 17, 2021 by Peter Minarik (86,040 points)
edited Oct 17, 2021 by Peter Minarik
" The result of Bitwise NOT is -2"

WRONG

The correct value is -21. Also, the output only shows the operators, but not the operands, so it's kind of meaningless. Even better if the binary value would be (also) displayed

E.g.:
"~20 (00010100) = -21 (11101011)"

or

"20 (00010100) & 21 (00010101) = 21 (00010101)"

etc...

Last, but not least, the code sample you copied here (through MS Word perhaps?!) does not contain the ASCII quote (") character, but some fancy alternative quotes (”, ”) that are not recognised by the compiler.
commented Oct 17, 2021 by Utpal Utkarsh (140 points)
Dear Sir, I have done amendments in my code as you suggested.  Thank you for your assistance.
commented Oct 17, 2021 by Peter Minarik (86,040 points)
All good. :)
0 votes
answered Oct 16, 2021 by Abdelatif-X-hub (140 points)
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a = 1100 0011
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...