Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Question about 2's complement system
Index -> General Discussion
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Voltemand11




PostPosted: Thu Sep 23, 2010 9:12 pm   Post subject: Question about 2's complement system

I'm doing a first year university assignment, and one of the questions says "Convert the negative 12 bit binary integer -110110110100 to its 2?s complement form and assume you are storing it in a 16 bit binary representation.". I know that in 2's complement form if the number starts with a 0 it is positive and if it starts with a 1 it is negative. I was just wondering if I could simply change the -110110110100 to 1110110110100 with the 1 in front to represent the negative symbol and change it to 1000110110110100 since it is being stored in 16 bits, or if I have to convert the 110110110100 to a positive base 10 number and put the negative symbol in front of it so it would be -3508 in base 10 (I just used a number base calculator to get the 3508), and then convert that number into 2's complement form. It's mildly confusing to me, so any help would be greatly appreciated. Thanks.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Fri Sep 24, 2010 12:33 am   Post subject: Re: Question about 2's complement system

Voltemand11 @ Thu Sep 23, 2010 9:12 pm wrote:
I know that in 2's complement form if the number starts with a 0 it is positive and if it starts with a 1 it is negative.

While true, there's more to it than that. What's the decimal value of 1000,0000 (two's complement, 8 bit integer).

Hint: it's not -0. http://en.wikipedia.org/wiki/Two's_complement
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Voltemand11




PostPosted: Fri Sep 24, 2010 8:46 am   Post subject: Re: Question about 2's complement system

Well I know the largest positive signed integer in 8 bits is 0111 1111, which is 127. If I convert that to a negative 2's complement integer it is 1000 0001 which is -127, so 1000 0000 would be -128.
Tony




PostPosted: Fri Sep 24, 2010 11:29 am   Post subject: Re: Question about 2's complement system

Right. Which should answer
Voltemand11 @ Thu Sep 23, 2010 9:12 pm wrote:
I was just wondering if I could simply change the -110110110100 to 1110110110100 with the 1 in front to represent the negative symbol and change it to 1000110110110100
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Voltemand11




PostPosted: Fri Sep 24, 2010 12:57 pm   Post subject: Re: Question about 2's complement system

Okay, so if I write 0110110110100 to denote +3508 in base 10, it's in 13 bits. I need to convert it to 2's complement to get the negative binary representation. If I use the rule where you start at the right of the binary number and go until you reach the first 1 and then flip every number after that, I get 1001001001100 which is -3508. But how do I put that into 16 bits?
Tony




PostPosted: Fri Sep 24, 2010 5:13 pm   Post subject: Re: Question about 2's complement system

Voltemand11 @ Fri Sep 24, 2010 12:57 pm wrote:
it's in 13 bits

Perhaps this is the point where you want to make it 16 bits long.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Voltemand11




PostPosted: Fri Sep 24, 2010 5:15 pm   Post subject: Re: Question about 2's complement system

Voltemand11 wrote:

But how do I put that into 16 bits?


I'm just not sure how to do that. Can I just add zeros after the 1 denoting the negative until there is 16 bits?
Tony




PostPosted: Fri Sep 24, 2010 7:18 pm   Post subject: Re: Question about 2's complement system

Voltemand11 @ Fri Sep 24, 2010 12:57 pm wrote:
+3508 in base 10, it's in 13 bits.

The number is positive.

You can't just add 0s to two's complement, as we've already established with
0000,0000
1000,0000
example.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Sponsor
Sponsor
Sponsor
sponsor
Voltemand11




PostPosted: Sat Sep 25, 2010 7:47 am   Post subject: Re: Question about 2's complement system

Okay, I think I've found the solution. In 16 bits +3508 would be 0000110110110100. If I convert it to -3508 it would be 1111001001001100. Thanks for your help, it was greatly appreciated!
Display posts from previous:   
   Index -> General Discussion
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: