Pergunta de entrevista da empresa Amazon

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

Respostas da entrevista

Sigiloso

28 de fev. de 2011

No one has handled the case for negative numbers. We need to represent negative numbers in either 1s or 2s compliment

1

Sigiloso

19 de jan. de 2011

// 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 > i)&1; } return binString; }

Sigiloso

27 de jan. de 2011

void bin_string(int data) { char binString[10]; int i=0; while(data!=0) { binString[i++]=((data>>1)&1)+48; data=data>>1; } binString[i]='\0'; cout<

Sigiloso

8 de fev. de 2011

static void bin_string(int n) { String bin = ""; int i = 0; do { bin += (char)(((n) & 1) + 48); n = n >> 1; } while (n != 0); }

Sigiloso

8 de fev. de 2011

You need to do a reverse string at the end

Sigiloso

13 de fev. de 2011

length = (int) (log (num) +1) ; // log to base 2 while ( (num/2) > 0)){ rem=num%2; binary[length-i-1] = rem; num=num/2 ; i--; }