Function to get no of set bits in binary representation of passed binary no. *
#include<stdio.h>
/* Function to get no of set bits in binary representation of passed binary no. */
int countSetBits(long n)
{
unsigned int num_zeroes = 0;
for(size_t i = 0; i < 8 * sizeof n; ++i, n >>= 1)
{
if ((n & 1) == 1)
++num_zeroes;
}
return num_zeroes;
}
int countResetBits(long n)
{
unsigned int num_zeroes = 0;
for(size_t i = 0; i < 8 * sizeof n; ++i, n >>= 1)
{
if ((n & 1) == 0)
++num_zeroes;
}
return num_zeroes;
}
/* Program to test function countSetBits */
int main()
{
long i = 5;
printf("%d", countResetBits(i));
printf("\n%d", countSetBits(i));
getchar();
}
//Decimal to Binary using Bitwise AND operator
void binary(unsigned int num)
{
unsigned int mask=32768; //mask = [1000 0000 0000 0000]
printf("Binary Equivalent : ");
while(mask > 0)
{
if((num & mask) == 0 )
printf("0");
else
printf("1");
mask = mask >> 1 ; // Right Shift
}
}
Output :
Enter Decimal Number : 10
Binary Eqivalent : 0000000000001010
Write a c program to multiply given number by 4
#include <stdio.h>
void main()
{
long number, tempnum;
printf("Enter an integer\n");
scanf("%ld",&number);
tempnum = number;
number = number << 2; /*left shift by two bits*/
printf("%ld x 4 = %ld\n", tempnum,number);
printf("Enter an integer\n");
scanf("%ld",&number);
tempnum = number;
number = number << 2; /*left shift by two bits*/
printf("%ld x 4 = %ld\n", tempnum,number);
}
/*------------------------------
Output
Enter an integer
15
15 x 4 = 60
RUN2
Enter an integer
262
262 x 4 = 1048
---------------------------------*/
C program to check odd or even using bitwise operator
#include<stdio.h>
main()
{
int n;
printf("Enter an integer\n");
scanf("%d",&n);
if ( n & 1 == 1 )
printf("Odd\n");
else
printf("Even\n");
return 0;
}
Write a ‘C’ program to swap two numbers using bitwise operator
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf("\n enter the elements\n");
scanf("%d%d",&x,&y);
printf("\n before swaping x=%d,y=%d",x,y);
x=x^y;
y=y^x;
x=x^y;
printf("\n after swaping x=%d y=%d",x,y);
getch();
}
Multiplication of two numbers using BITWISE operators
//How will you multiply two numbers using BITWISE operators
#include<stdio.h>
main()
{
int a,b,result;
printf("nEnter the numbers to be multiplied :");
scanf("%d%d",&a,&b);
result=0;
while(b != 0) // Iterate the loop till b==0
{
if (b&01) // Logical ANDing of the value of b with 01
result=result+a; // Update the result with the new value of a.
a<<=1; // Left shifting the value contained in 'a' by 1.
b>>=1; // Right shifting the value contained in 'b' by 1.
}
printf("nResult:%d",result);
}
Bitwise operation for division
int main(void) {
int L, leftShift, i, changeMask;
leftShift = 0; /* added or subtracted value will be 2^leftShift */
i = 25; /* value we are adding to or subtracting from */
changeMask = 1 << leftShift;
for (L = leftShift; L < INT_BIT; L++) {
i ^= changeMask;
if ( /* ! */ (i & changeMask)) { /* comment in or out "!" for
addition or subtraction */
break;
}
changeMask <<= 1;
}
printf("%i", i);
return 0;
}
Program in C to manipulate bits,bitmanip.c
//========================
//Program in C to manipulate bits,bitmanip.c
//========================
#include<stdio.h>
int main()
{
int n,b;
printf("\n Enter a number and bit to manipulate: \n");
scanf("%d%d",&n,&b);
printf("You entered %d and %d",n,b);
//Code to Set bth bit in number n
n=n|(1<<b);
//to toggle //n=n^(1<<b)
//to display bth bit// n&(1<<b)
//to clear bth bit // n& ~(1<<b)
printf("\n Entered Number after setting %d th bit is %d \n :",b,n);
return 0;
}
Program in C to set nth bit, bitset.c
//========================
//Program in C to set nth bit, bitset.c
//========================
#include<stdio.h>
int showbits(int nn)
{
unsigned int m;
m=1<<(sizeof(nn)*8-1);
while(m > 0)
{
if(nn & m)
{
printf("1");
}
else
{
printf("0");
}
m>>=1;
}
}
int main()
{
int number,bits;
printf("Enter a nomber and bits to set:\n");
scanf("%d%d",&number,&bits);
printf("\n You Entered: %d and %d \n",number,bits);
showbits(number);
number=number^(1<<(bits-1));
printf("\n Number now is: %d \n",number);
showbits(number);
printf("\n");
return 0;
}
Program in C to check bit at nth position, bitcheckatn.c
//========================
//Program in C to check bit at nth position, bitcheckatn.c
//========================
#include<stdio.h>
int showbits(int nn)
{
unsigned int m;
m=1<<(sizeof(nn)*8-1);
while(m > 0)
{
if(nn & m)
{
printf("1");
}
else
{
printf("0");
}
m>>=1;
}
}
int main()
{
int number,bits;
printf("Enter a nomber and bits to see qt bit th position:\n");
scanf("%d%d",&number,&bits);
printf("\n You Entered: %d and %d \n",number,bits);
showbits(number);
number=number&(1<<(bits));
printf("\n Now number is :%d and \n bit at %d position is: \n",number,bits);
showbits(number);
printf("\n");
return 0;
}
How to Swap two numbers using bitwise operator?
Answer:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf("\n enter the elements\n");
scanf("%d%d",&x,&y);
printf("\n before swaping x=%d,y=%d",x,y);
x=x^y;
y=y^x;
x=x^y;
printf("\n after swaping x=%d y=%d",x,y);
getch();
}
Entered Character is digit or uppercase alphabet or lowercase alphabet
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("enter the character\n");
scanf("%c",&ch);
if(ch>='0'&& ch<='9')
printf("\nIt is adigit");
if(ch>='A'&&ch<='Z')
printf("\n It is alphabet");
if(ch>='a'&&ch<='z')
printf("\nit is alphabet");
getch();
}
Sum of digit of a number using recursion function
#include<stdio.h>
#include<conio.h>
void main()
{
int sumdig(int);
int n,sum,d,f,rev=0;
clrscr();
printf("enter the number");
scanf("%d",&n);
sum=sumdig(n);
printf("%d",sum);
getch();
}
int sumdig(int n)
{
int sum,d,r=0;
if(n==0)
return(0);
else
sum=n%10+sumdig(n/10);
while(sum!=0)
{
d=sum%10;
r=r+d;
sum=sum/10;
}
return(r);
}
Write a ‘C’ program to convert given decimal number into binary number
#include
#include
void main()
{
int n,j,a[50],i=0;
clrscr();
printf("\n enter the value :-");
scanf("%d",&n);
while(n!=0)
{
a[i]=n%2;
i++;
n=n/2;
}
printf("\n binary conversion\n");
for(j=i-1;j>=0;j--)
printf("%d",a[j]);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int n,tmp,d,rev=0;
clrscr();
printf("enter the number=");
scanf("%d",&n);
tmp=n;
while(n!=0)
{
d=n%10;
rev=(rev*10)+d;
n=n/10;
}
if(tmp==rev)
printf("enter number is palindrom");
else
printf("enter number is not palindrom");
getch();
}