## Amazon Interview Question

Country: India

Comment hidden because of low score. Click to expand.
2
of 4 vote

@Nascent

Do we need to compute the two's compliment form and print it out as a string? Otherwise, since a computer anyway stores a number in two's compliment form, we can just print out the bits of the memory word.

Comment hidden because of low score. Click to expand.
2

isn't two compliement of a negative number is the postive number itself.

int main()
{
int x=-1;
printf("%x\n", x);
printf("%x\n", ~x+1);
}

Comment hidden because of low score. Click to expand.
0

Comment hidden because of low score. Click to expand.
1
of 1 vote

2's complement of (-x) is (x) itself as said by everybody.
Just multiply the number by (-1) or do (~x + 1) to get the result.

Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess the logic is just flip all the digits and add 1 to the result

Comment hidden because of low score. Click to expand.
0
of 0 vote

2's complement of a negative number is simply a positive magnitude of that number. For example, if number is -6 its 2's complement will be 6.
I think this is the code

``````#include <stdio.h>

main() {
int a=-6;
a = (~a)+1;
printf("%d\n",a);
}``````

Comment hidden because of low score. Click to expand.
1
of 1 vote

it can also be
no = no * -1;
or
no = -(no)
:)

Comment hidden because of low score. Click to expand.
0

:) yes ofcourse

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````public class TwosComplement {
public static void main(String[] args) {
int no = -8;
printBits(no);
printBits(~no);
printBits(~no + 1);
}

public static void printBits(int no) {
System.out.println("\nBinary format of:" + no + "\n"
+ Integer.toBinaryString(no));
for (int i = 31; i >= 0; i--)
System.out.print((((no & (1 << i)) >>> i) > 0) ? "1" : "0");
}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

I would suggest that there be more clarity on the input. One possible cpp solution.

``````#include<iostream>
#include<stdlib.h>
#include<cstring>
using namespace std;
//assumptions:
//1. input is a binary number in a char string
int main(void)
{
string str;
cin >> str;
//flip the digits
cout << str << endl;
for (int i=0;i<str.size();i++)
str[i] = ((str[i] == '0') ? '1' : '0');
cout << str <<endl;
for(int i = str.size();i>=0;i--)
if (str[i] == '0')
{
str[i] = '1';
break;
}
else
{
str[i] = '0';
continue;
}
cout << str <<endl;
return 0;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````#include <iostream>
using namespace std;

int main() {
int n;
while(cin>>n){
cout<<n<<" : "<<(~n)+1<<endl;
}
return 0;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
main(){

int a,b,k;

printf("enter the value");
scanf("%d",&a);

if(a>0){
k=~a+1;
printf(" \n %d 2's complement %d",a,k);
}
else{
k=0xff;
b=k^a;
printf("\n %d 2's complement %d",a,b);
}

}

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main(){
int a = -95;
printf("%d",(255^abs(a))+1);
getch();
}``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.