Wednesday, 20 March 2013

Int to Binary String

//intToBinary.cpp
//HYQ
//Mar 20,2013

const char* bits(unsigned int val){
    int i;
    unsigned int m;
    char* v = new char[(sizeof(val)*8)+1];
    int c=48;//ascII char '0' 

    for(i=0,m=1<<(sizeof(val)*8-1);
      m;//until 0
      v[i]=(c+!!(val&m)),m=m>>1,i++);
    v[i]=0;
    return v;
}

No comments:

Post a Comment