0% found this document useful (0 votes)
32 views

09a Bitwise Operators - All Programs

The document contains C++ programs that demonstrate various bitwise operators including AND, OR, NOT, left shift, and right shift. It shows how to use these operators to turn bits on or off in a number, extract date values stored in a single byte, and test whether a particular bit is on or off. The programs include functions to display the binary representation of numbers.

Uploaded by

darshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

09a Bitwise Operators - All Programs

The document contains C++ programs that demonstrate various bitwise operators including AND, OR, NOT, left shift, and right shift. It shows how to use these operators to turn bits on or off in a number, extract date values stored in a single byte, and test whether a particular bit is on or off. The programs include functions to display the binary representation of numbers.

Uploaded by

darshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PDPU, Gandhinagar 09 Bitwise Operators – All Programs

Showbin.h

void showbin(int n)
{
int i, k , andmask;
for(i=15;i>=0;i--)
{
andmask = 1 << i;
k = n & andmask;
k == 0?cout<<0:cout<<1;
};
}

Bitwise1.cpp

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include “showbin.h”

void main()
{
clrscr();
void showbin(int);
int i,j;
for(i=0;i<=5;i++)
{
cout << "\t" << i << "(10) = ";
showbin(i);
cout << "(2)" << endl;
};
getch();
}

Bitwise2.cpp

// the use of or operator. |.


// to put ON a particular bit in a number.
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include “showbin.h”

By Darshit Shah 1
PDPU, Gandhinagar 09 Bitwise Operators – All Programs

void main()
{
clrscr();
void showbin(int);
int i = 65, j;
cout << "\n\n\ti = " << i << " = " ;
showbin(i);
cout << endl;
j = i | 32;
cout << "\tj = " << j << " = " ;
showbin(j);
cout << endl;
j = i | 8;
cout << "\tj = " << j << " = " ;
showbin(j);
getch();
}

Bitwise3.cpp

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include “showbin.h”

void main()
{
clrscr();
void showbin(int);
int i,j;
for(i=0;i<=5;i++)
{
cout << "\n\t" << i << "(10) = ";
showbin(i);
cout << "(2)\t1's complement = ";

j = ~i;
showbin(j);
};
getch();
}

By Darshit Shah 2
PDPU, Gandhinagar 09 Bitwise Operators – All Programs

Bitwise4.cpp

// right shift operator >>.


#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include “showbin.h”

void main()
{
clrscr();
void showbin(int);
int i,j,n=5470;
cout << "\n\n\t\tTHE USE OF RIGHT SHIFT >> \n\n\t\t\t" << n
<< "(10) = ";
showbin(n);
for(i=0;i<=5;i++)
{
j = n>>i;
cout << "\n\t right shift by " << i << " = ";
showbin(j);
cout << " =(" << j <<")10";
};
cout << endl<< endl << "\tNote that the digit from right
side is lost ";
cout << endl<< "\twhile digit from left side is filled with
Zeros.";
cout << endl<< "\n\n\tIF THE OPERAND IS A MULTIPLE OF 2 THEN
SHIFTING";
cout << endl<< "\tTHE OPERAND BY ONE BIT TO RIGHT IS SAME AS
DIVIDING";
cout << endl<< "\tIT BY 2 AND IGNORING THE REMAINDER.";

getch();
}

Bitwise5.cpp

// left shift operator <<.


#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include “showbin.h”

By Darshit Shah 3
PDPU, Gandhinagar 09 Bitwise Operators – All Programs

void main()
{
clrscr();
void showbin(int);
int i,j,n=1000;
cout << "\n\n\t\tTHE USE OF LEFT SHIFT << \n\n\t\t\t" << n
<< "(10) = ";
showbin(n);
for(i=0;i<=5;i++)
{
j = n<<i;
cout << "\n\t Left shift by " << i << " = ";
showbin(j);
cout << " =(" << j <<")10";
};
cout << endl<< endl << "\tNote that the digit from left side
is lost ";
cout << endl<< "\twhile digit from right side is filled with
Zeros.";
cout << endl<< "\n\n\tLEFT SHIFT OPERATOR GIVES THE RESULT
AS IF";
cout << endl<< "\tMULTIPLYING OPERAND BY 2.";

getch();
}

Bitwise6.cpp

// A PRACTICAL UTILITY OF BIT WISE OPERATORS.


// DATE STORED BY DOS/WINDOWS IN ONE BYTE.
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include “showbin.h”

void main()
{
clrscr();
unsigned int d = 30, m = 10 , y = 1990 , year, month, day,
date;
void showbin(int);

date = ( y - 1980) * 512 + m * 32 + d;


cout << "\n\t\tDate = " << date;
year = 1980 + (date >> 9);

By Darshit Shah 4
PDPU, Gandhinagar 09 Bitwise Operators – All Programs

month = ((date << 7) >> 12);


day = ((date << 11) >> 11);
cout << "\n\t\tYear = " << year;
cout << "\n\t\tMonth= " << month;
cout << "\n\t\tDay = " << day;
cout << "\n\n\t(2) = ";
showbin(date);
getch();
}

Bitwise7.cpp

// to test whether a bit in a number is ON or OFF.


#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include “showbin.h”

void main()
{
clrscr();
void showbin(int);
int i = 65, j;
cout << "\n\n\ti = " << i << " = " ;
showbin(i);
cout << endl;
j = i & 32;
if ( j == 0)
cout << "\n\tIts fifth bit is off." << endl;
else
cout << "\n\tIts fifth bit is on." << endl;
j = i & 64;
if ( j == 0)
cout << "\n\tIts sixth bit is off." << endl;
else
cout << "\n\tIts sixth bit is on." << endl;

getch();
}

By Darshit Shah 5

You might also like