This is my version of the three functions
void prnBits(unsigned int Val){
for (unsigned int i=(1<<(sizeof(Val)*8-1));i;printf("%d", !!(Val & i)),i>>=1); // in one line
/* original function
unsigned int m = 1 << sizeof(Val) * 8 -1;
while(m){
printf("%d", !!(Val & m));
m = m >> 1;
}
*/
}
char* bits(unsigned int Val){
unsigned int m = 1 << sizeof(Val) * 8 -1;
char* ret=new char[sizeof(Val)*8+1];
int i=0;
while(m){
ret[i]=!!(Val&m)+48;
m>>=1;
i++;
}
ret[i]=0;
return ret;
}
// write the above function so it works like this:
// cout<<bits(2345)<<endl;
void bitDump(void* memAddress, unsigned int memSize){
int i;
int* a=(int*)memAddress;
for (i=0;i<memSize;i++){
prnBits(a[i]);
}
}
No comments:
Post a Comment