C Program to convert number to binary using bitwise operators
#include
#include
void
printbinary(int num)
{
int
mask=0x4000;
if((num&0x8000)==0)
printf("0");
else
printf("1");
while(mask!=0)
{
if((num&mask)==0)
printf("0");
else
printf("1");
mask=mask>>1;
}
}
void main()
{
int intnum;
printf("\n enter a integer
number: ");
scanf("%d",&intnum);
printf("\n integer number in binary format:
");
printbinary(intnum);
getch();
}
Output
Enter a integer number :91
Integer number in binary format : 0000000001011011
C Program to demonstrate Bitwise right shift operator
#include
#include
void
printbinary(int num)
{
int
mask=0x4000;
if((num&0x8000)==0)
printf("0");
else
printf("1");
while(mask!=0)
{
if((num&mask)==0)
printf("0");
else
printf("1");
mask=mask>>1;
}
}
void main()
{
int intnum,
shiftednumber,n;
printf("\n right shift
operation:”);
printf("\n enter a integer number:”);
scanf("%d",&intnum);
printf("\nplaces by which you want to right
shift: ");
scanf("%d",&n);
shiftednumber=intnum>>n;
printf("\n original number: ");
printbinary(intnum);
printf("\ number after right shifting : “);
printbinary(shiftednumber);
getch();
}
Output
Right shift operation:
Enter a integer number :88
Places by which you want to right shift : 3
Original number :0000000001011000
Number after right shifting : 0000000000001011
C Program to Swap two numbers using XOR Operator
#include
#include
int main()
{
int num1,num2;
printf("\n enter first number: ");
scanf("%d",&num1);
printf("\n enter second number : ");
scanf("%d",&num2);
num1=num1^num2;
num2=num1^num2;
num1=num1^num2;
printf("\n numbers after exchange : ");
printf("\n num1=%d and
num2=%d",num1,num2);
return(0);
}
Output
Enter first number :
20
Enter second number :40
Number after exchange : num1=40 and num2 = 20
Number after exchange : num1=40 and num2 = 20
C Program to demonstrate Bitwise left shift operator
#include
#include
void
printbinary(int num)
{
int
mask=0x4000;
if((num&0x8000)==0)
printf("0");
else
printf("1");
while(mask!=0)
{
if((num&mask)==0)
printf("0");
else
printf("1");
mask=mask>>1;
}
}
void main()
{
int intnum,
shiftednumber,n;
printf("\n left shift
operation:”);
printf("\n enter a integer number:”);
scanf("%d",&intnum);
printf("\nplaces by which you want to left
shift: ");
scanf("%d",&n);
shiftednumber=intnum<
printf("\n original number: ");
printbinary(intnum);
printf("\ number after left shifting : “);
printbinary(shiftednumber);
getch();
}
Output
Left
shift operation:
Enter
a integer number : 45
Places
by which you want to left shift : 3
Original
number :0000000000101101
Number
after left shifting : 0000000101101000
No comments:
Post a Comment