Bitwise Operators in C Programming: AND, XOR, OR, Complement and Shift Operations - Codzify Tutorials

2 min 31 sec read Basic


There are many Bitwise operators in C as defined below:

& (bitwise AND) :- Takes two numbers as operand and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.

| (bitwise OR) :- Takes two numbers as operand and does OR on every bit of two numbers. The result of OR is 1 any of the two bits is 1.

^ (bitwise XOR) :- Takes two numbers as operand and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.

<< (left shift) :- Takes two numbers, left shifts the bits of first operand, the second operand decides the number of places to shift.

>> (right shift) :- Takes two numbers, right shifts the bits of first operand, the second operand decides the number of places to shift.

~ (bitwise NOT) :- Takes one number and inverts all bits of it.

Rule :-

& = 1 & 1 is 1 else 0.

| = any of two bit is 1 then 1 else 0.

^ = if both bits are different then 1 else 0.

a>> 1 = shift the bits of a to right by 1 and replace the first bit from left by 0 .

a>> 2 = shift the bits of a to right by 2 and replace the first two bits from left by 0 .

a<< 1 = shift the bits of a to left by 1 and replace the last bit from right by 0 .

a<< 2 = shift the bits of a to left by 2 and replace the last two bits from right by 0 .

~a = replace 0 by 1 and 1 by 0.

Example :-

                a= 5    =    00000101
                b= 9    =    00001001
                覧覧覧覧覧覧覧覧覧覧
                a&b     =    00000001
                a|b     =    00001101
                a^b     =    00001100
                a>>1    =    00000100
                a>>1    =    00001010
                ~a      =    11111010
                


Codzify's

1:1 & 1:Many Mentorship Program

Be job-ready. Learn how to crack the interviews with industry ready training program.


Learn More

Data Structures & Algorithms

What is Dynamic Programming and how to use it

This is a quick introduction to Dynamic Programming.

3 ways to check anagrams

Let's discuss the 3 ways to check anagrams and their time complexity.

Bubble Sort Algorithm

Topics covered: 1) Basics of Bubble Sort Algorithm 2) Worst Case, Best Case complexity

Introduction to Trees

What is a Tree and Terminologies like root, sibling, parent node, children, Leaf node, Ancestor & Descendant, Properties of Tree Data Structure, Depth of node, Height of node

Address Calculation in 2d array

Let's discuss about the Address Calculation using row-major order & column-major order.

Javascript

Javascript - Postfix Vs Prefix

Let's discuss the difference between postfix and prefix with a practical example.

Javascript Identifiers, Variables

In this session, we will discuss the concept of identifiers, variables and operators in javascript with a practical example.

Bootstrap

Bootstrap 4 Tutorial: Modal Popups Made Easy | Live Demo

Let's discuss the Modal Popups in Bootstrap.

How to create a dropdown list in navbar bootstrap

In this video, we will discuss How to create a dropdown list in navbar bootstrap with a practical example.

Navigation tabs in Bootstrap

Let's discuss about the navigation tabs in Bootstrap

Bootstrap Buttons

Let's discuss about the button classes in bootstrap.


Recommended tutorials