March 12, Lab
printing bitrate
#include <iostream>
using namespace std;
char* bit(unsigned int);
char isOn(unsigned int, unsigned int);
int main(){
int test=10;
cout << sizeof(int) << endl;
cout << sizeof(test) << endl;
cout << bit(test) << endl;
return 0;
}
char* bit(unsigned int i){
static char binary[31]; //int is 4 bytes meaning 32 bits
int num;
int c;
for(c=sizeof(i)*8-1,num=0;c>=0,num < sizeof(i)*8;c--,num++)
binary[num]=(isOn(i,c));//num increments the array of binary while c looks at every bitrate one by one
return binary;
}
char isOn(unsigned int num, unsigned int bitNo){
unsigned int m=1 << bitNo;
if ((num & m) != 0)
return '1';
else
return '0';
}
No comments:
Post a Comment