Welcome~~~


Another blog:
http://fun-st.blogspot.com/

It is easier to write an incorrect program than understand a correct one.

Tuesday, February 8, 2011

Write a function to obtain a string which is the binary representation of an integer

// it's caller's responsibility to delete the memory
char * conv_int_to_binary(int data)
{
    int size = sizeof(data)*8;
    char *binString = new char(size);

   for ( int i = 0; i < size; i++)
     {
           binString[i] = (data >> i)&1;
     }
  return binString;
}

reverse string at the end

or --> start from binString[size], with size --

From: http://www.glassdoor.com/Interview/Write-a-function-to-obtain-a-string-with-the-binary-representation-of-an-integer-QTN_132949.htm

No comments:

Post a Comment