Introduction to Newbie Programmer Series Introduction to Computer The Computer Programming Language Setting up the Computer for Programming
 Lets Get Started !
Beginning C Programming Variables a Handbag ! Basic Variable types in C Basic Formatted Output Basic Formatted Input
Looping Repeating Concept Different Kinds of Loops Advanced concepts and trick with Loops
Operators, An Introduction Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Miscellaneous Operators and Operators Precedence
 Controlling the Flow
Flow Charts Conditional Statements Using Goto and Label Break and Continue the loops Switch the Cases Some Practical Applications of Control Flow
 The program structure
The program Structure Functions More Concepts of Functions Scope of the Variables across functions How the C language itself Works ? Scope of the Variables across files Static Variables Auto and Register Variables #define PreProcessor Some more PreProcessors
 Arrays and Structures
Introduction to Arrays Arrays, some more Concepts Arrays, Cool Examples Structures Introduction Structure, Some more Concepts Structure, Some Examples
 File Management
Basics of File management in C Steps Involved with File Handling in C
Inside Logic Gates – The Electronic Logic Binary Number System Mathematical Reasoning
Newbie Programmer
full book coming soon
by Shubham Ramdeo

Zeros and ones ! Bits ! They can be shifted and played with too using the Bitwise Operators !


Bitwise Operators

Welcome to the Newbie Programmer Series. We were discussing different types of operators and Bitwise Operators are of them. But Before I started writing this, I need to make some pre-required posts so that I can make Bitwise Operators easy to understand. We will discuss all of them now below. If you are new to this series, please go to index (click here) and read out all the previous articles so that you can easily understand what is going on below.

Bitwise Operators are very useful but their applications are very complex. I am not going to any deep detail.

This post will only help you to get a little introduction of them, we will not study deeply, just to introduce ourselves what they are.

Before Going into Bitwise Operators, Please checkout :

Now let's get started.
The bitwise Operators are used to perform operations on the Bits. Yes those zeros and ones. By using bitwise operators, we can manipulate the ones and zeros of a value. For example we have discussed many times the OR operators. It give '1' in result when there exist at least one '1'. So on performing :
OR   110100011
ans.   110100111
But before discussing them let make some variables :
A = 0011 1100 = 60
B = 0000 1101 = 13
(These above variables are suggested by a friend of mine, not my idea )
If you have read the Binary Number System post linked above, You can easily get what zeros and ones would mean.
If you have read the Mathematical Reasoning post linked above, You can easily get what the following operators Below would mean :

Binary AND operator ( & )

If both bits are 1 then resulting bit is 1 otherwise 0

So here: A&B =

0011 1100

0000 1101


0000 1100

which gives (A&B) = 13 as 00001100 means 13 in Binary.

Binary OR operator ( | )

If at least one bit is 1 then resulting bit is 1. Only when both bits are 0 then resulting bit is 0.
So here: A|B =
0011 1100

0000 1101


0011 1101

which gives (A|B) = 61

Binary XOR operator ( ^ )

Resulting bit is '1' only if it is set in one of the operand but not both.

So here: A^B =

0011 1100

0000 1101


0011 0001

which gives (A^B) = 49

Binary NOT ( or complement ) operator ( ~ )

Its simple gives complement of the bits. It reverses ones to zeros and zeros to one.
So here ( ~ A ) = 1100 0011 = -61

Binary Left Shift Operator ( << )

The values of the bits are shifted left by the number of specified shifts. So for example,
A = 0011 1100
A >> 2 = 1111 0000
All the bits got left by 2 spaces.

Binary Right Shift Operators ( >> )

The values of the bits are shifted right by the number of specified shifts. So for example,
A = 0011 1100
A << 2 = 0000 1111

Using with assignment

Remember the following shortcuts :

C &= 2 is same as C = C & 2

C |= 2 is same as C = C | 2

C ^= 2 is same as C = C ^ 2

C <<= 2 is same as C = C << 2

C >>= 2 is same as C = C >> 2

That's it for today. I will do NO programs for now as these things are already complex but we need to do an intro. I am gonna end operators with next post.

So Stay Connected.


Please share this as much as you can so that we can get connected with more and more people who want to learn programming.

© Shubham Ramdeo

Site Map
© Shubham Ramdeo