Page - 1
Page - 1
Page - 1
WAP to find the given number is even or odd using bitwise operators. ...................... 5
WAP to find the given number is +ve or -ve using bitwise operators.......................... 6
WAP to find the given number is divisible by 8 or not using bitwise operators. .......... 9
Write a program to rotate the bits. Input the no of rotations, at runtime. ................ 10
Convert the characters Upper to Lower & Lower to Upper using bitwise operators. 11
Write a one line code to compare two numbers using bitwise operators. ................. 14
Write a program to print float binary formation using char *ptr. ................................ 15
Write a program to swap the adjacent bytes of a given 4-digit hex number. ............ 17
Write a program to delete no of bits from particular position in a given number. ...... 19
Write a macro for swapping first and last nibbles in a given integer. ........................ 19
There are 48 bits are stored in an array of character buffer and store them into 2
Page | 1
Bitwise Operators
#include<stdio.h>
main()
int num,pos,r;
scanf("%d %d",&num,&pos);
Page | 2
Write a program for the following one.
#include<stdio.h>
main()
int num,pos,r;
char op;
scanf("%d %d",&num,&pos);
printf("Enter Choice\n");
scanf(" %c",&op);
switch(op)
case 's':
printf("\nAnswer:%d\n",num);
break;
case 'r':
Page | 3
num = num & ~(1 << pos);
printf("\nAnswer:%d\n",num);
break;
case 'c':
printf("\nAnswer:%d\n",num);
break;
default:
printf("\nInvalid Chice\n");
Page | 4
WAP to find the given number is even or odd using bitwise operators.
#include<stdio.h>
main()
int num;
printf("Enter Number:\n");
scanf("%d",&num);
(num % 2)?printf("Odd\n"):printf("Even\n");
Page | 5
WAP to find the given number is +ve or -ve using bitwise operators.
#include<stdio.h>
main()
int num;
printf("Enter Number\n");
scanf("%d",&num);
if(num < 0)
printf("Negative Number\n");
else
printf("Positive Number");
Page | 6
WAP to swap two numbers using bitwise operators.
#include<stdio.h>
main()
int num1,num2;
scanf("%d %d",&num1,&num2);
printf("\nBEFORE : %d \t%d\n",num1,num2);
printf("AFTER : %d \t%d\n",num1,num2);
Page | 7
WAP to find the given number is power of 2 or not.
#include<stdio.h>
main()
int num;
printf("Enter Number\n");
scanf("%d",&num);
Page | 8
WAP to find the given number is divisible by 8 or not using bitwise
operators.
#include<stdio.h>
main()
int num;
printf("Enter Number\n");
scanf("%d",&num);
Page | 9
Write a program to rotate the bits. Input the no of rotations, at
runtime.
Ex : binary : 10000000000000000000000000001011
result : 01110000000000000000000000000001
binary : 10000000000000000000000000001011
result : 00000000000000000000000010111000
Page | 10
Convert the characters Upper to Lower & Lower to Upper using
bitwise operators.
#include<stdio.h>
main()
char n;
printf("Enter Character\n");
scanf("%c",&n);
n = n ^ 32;
Page | 11
Write a program to reverse the bits of a given number.
#include<stdio.h>
main()
int num,i,j;
printf("Enter Number\n");
scanf("%d",&num);
for(i=sizeof(int)*8-1;i>=0;i--)
if (i % 8 == 0)
printf(" ");
printf("\n");
for(i=0,j=sizeof(int)*8-1;i<(sizeof(int)*8)/2;i++,j--)
Page | 12
{
for(i=sizeof(int)*8-1;i>=0;i--)
if (i % 8 == 0)
printf(" ");
printf("\n");
Page | 13
Write a one line code to compare two numbers using bitwise
operators.
#include<stdio.h>
main()
int n1,n2;
scanf("%d %d",&n1,&n2);
n1 ^ n2 ? printf("Different\n"):printf("Same\n");
Page | 14
Write a program to print float binary formation using char *ptr.
#include<stdio.h>
main()
float num;
char *c=#
int i,j;
scanf("%f",&num);
c=c+3;
for(i=0;i<4;i++)
for(j=7;j>=0;j--)
printf(" ");
c=c-1;
printf("\n");
Page | 15
Page | 16
Write a program to swap the adjacent bytes of a given 4-digit hex
number.
#include<stdio.h>
main()
int i,j,k,num,m,n;
printf("Enter Number\n");
scanf("%d",&num);
printf("%x\n",num);
for(i=sizeof(int)*8-1;i>=0;i--)
if(i % 8 == 0)
printf(" ");
printf("\n");
for(i=0,j=8;i<=7;i++,j++)
Page | 17
{
for(i=sizeof(int)*8-1;i>=0;i--)
if(i % 8 == 0)
printf(" ");
printf("\n%x",num);
printf("\n");
Page | 18
Write a program to delete no of bits from particular position in a
given number.
Write a macro for swapping first and last nibbles in a given integer.
There are 48 bits are stored in an array of character buffer and store
them into 2 integer variables.
Page | 19