Ref for Several BIT Manipulation type and techniques based Example :
http://graphics.stanford.edu/~seander/bithacks.html
for ex :
Q 1. Write a program to count 1's in the given integer ?
Lets say we are using 32 bit - little endian processor. User enters any integer value and we need to find the number of bit 1 in that integer.
Example: integer n =(10) base 10 equals to (0000 0000 0000 0000 0000 0000 0000 1010) base 2. so there are 2 1's bits in this . so the Output should be 2.
Program:
#include
#include conio.h
int main()
{
int a,b=1,c=0; //lets say a = 12
printf("Enter the integer: ");
scanf("%d",&a);
while(a)
{
if(a&b)
c++;
a=a>>1;
}
printf("number of 1: %d",c);
getch();
return 0;
}
// Note: it will change the Value of original provided integer to 0.
No comments:
Post a Comment