09a Bitwise Operators - All Programs
09a Bitwise Operators - All Programs
09a 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
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
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
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
void main()
{
clrscr();
unsigned int d = 30, m = 10 , y = 1990 , year, month, day,
date;
void showbin(int);
By Darshit Shah 4
PDPU, Gandhinagar 09 Bitwise Operators – All Programs
Bitwise7.cpp
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