Monday 12 March 2012

Return the bits present in a integer by using a function

#include<iostream>
using namespace std;
#define len sizeof(int)*8

char* bits(unsigned int j) {
    int c = 0;
    int cur=0;
    int i=sizeof(int)*8;
    static char str[len+1] = {'\0'};
    while(c <= i-1) {
        cur = j<<(i-c-1)>>(i-1);
        if(cur==1) str[i-1-c] = '1';
        else str[i-1-c]= '0';
        c++;
    }
return str;
}

int main() {
    int i = 2345;
    cout<<"i: "<<bits(i)<<endl;
    return 0;
}

No comments:

Post a Comment