/* Bit finder */
/*Program to find out whether a bit in a
given no is switched ON(1) or OFF(0).
The search is only limited till the
seventh bit of the number*/
/*Author:Ariant A goswami
EMAIL: apache_elvis@yahoo.com
Suggestion or comments(good or bad) are welcomed.
*/
#include<iostream.h>
#include<conio.h>
#include<math.h>
/*functions are declared as void */
void firstbit_info(int no);// to get information about the first bit of the given number
void secondbit_info(int no);// to get information about the second bit of the given number
void thirdbit_info(int no);// to get information about the third bit of the given number
void fourthbit_info(int no);// to get information about the fourth bit of the given number
void fifthbit_info(int no);// to get information about the fifth bit of the given number
void sixthbit_info(int no);// to get information about the sixth bit of the given number
void seventhbit_info(int no);// to get information about the seventh bit of the given number
/*main function*/
void main(){
int num,choice;
clrscr();
cout<<"[Bit Finder]\n";
cout<<"Enter a number:";
cin>>num;
cout<<"which bit do you want to know about(1,2,..7):";
cin>>choice;
/*depending on the users choice the value of num is passed on to the respective function*/
switch (choice){
case 1: firstbit_info(num);
break;
case 2:secondbit_info(num);
break;
case 3:thirdbit_info( num);
break;
case 4:fourthbit_info(num);
break;
case 5:fifthbit_info(num);
break;
case 6:sixthbit_info(num);
break;
case 7:seventhbit_info(num);
}
getch();
}
/* function to get info about the first bit*/
void firstbit_info(int no){
int bf;
bf=pow(2,1);
no=no&bf;/*BitWISE AND OPERATOR IS USED TO FIND WHETHER BIT IS ON OR OFF*/
no==0?cout<<"bit is off(0)":cout<<"bit is on(1)";
}
/*the rest of the fuctions work in the same way except the value of bf increases
with respect to the bit number */
void secondbit_info(int no){
int bf;
bf=pow(2,2);//value of bf increases 2*2=4
no=no&bf;
no==0?cout<<"bit is off(0)":cout<<"bit is on(1)";
}
void thirdbit_info(int no){
int bf;
bf=pow(2,3);//value of bf increases 2*2*2=8
no=no&bf;
no==0?cout<<"bit is off(0)":cout<<"bit is on(1)";
}
void fourthbit_info(int no){
int bf;
bf=pow(2,4);//value of bf increases 2*2*2*2=16 and so on...
no=no&bf;
no==0?cout<<"bit is off(0)":cout<<"bit is on(1)";
}
void fifthbit_info(int no){
int bf;
bf=pow(2,5);
no=no&bf;
no==0?cout<<"bit is off(0)":cout<<"bit is on(1)";
}
void sixthbit_info(int no){
int bf;
bf=pow(2,6);
no=no&bf;
no==0?cout<<"bit is off(0)":cout<<"bit is on(1)";
}
void seventhbit_info(int no){
int bf;
bf=pow(2,7);
no=no&bf;
no==0?cout<<"bit is off(0)":cout<<"bit is on(1)";
}