Why is ~4 -3? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is ~4 -3?

Can someone explain me why it works this way? If I flip all the bits, It seems like I would get a very big number. My questions are: 1. What is 1's and 2's complement? 2. How does the computer know which one is used? 3. Does the amount of bits give me another answer of ~4 (or any other number)?

21st May 2018, 9:47 AM
Lucas Knook
Lucas Knook - avatar
13 Answers
+ 8
take a look at this lesson and the code the key to your answer lies in how numbers are represented by a computer and what is the MSB (research it ;) ) https://www.sololearn.com/learn/4076/?ref=app https://code.sololearn.com/WBOdrCZZT0a7/?ref=app
21st May 2018, 9:51 AM
Burey
Burey - avatar
+ 6
nope the left most bit (MSB) determines the sign of the number (1 for negative, 0 for positive) and numbers with a minus sign numbers it works the opposite (almost) -1 for example is a composed of only 1's -1 1111 1111 1111 1111 1111 1111 1111 1111 -2 1111 1111 1111 1111 1111 1111 1111 1110 -3 1111 1111 1111 1111 1111 1111 1111 1101 and so on
21st May 2018, 10:02 AM
Burey
Burey - avatar
+ 2
Look into twos complement if you want to know more
21st May 2018, 10:03 AM
Max
Max - avatar
+ 1
Lucas Knook you are correct. All modern processors use twos complement, it hardwired in their ALU. Only legacy systems use ones complement. See this for more info https://superuser.com/questions/1137182/is-there-any-existing-cpu-implementation-which-uses-ones-complement
21st May 2018, 5:06 PM
Max
Max - avatar
0
yes, I have seen these, and I understand what MSB means, but still, I don't understand.
21st May 2018, 9:55 AM
Lucas Knook
Lucas Knook - avatar
0
when I put in 5, I get this as result: 1111 1111 1111 1111 1111 1111 1111 1010 Shouldn't this be a very big number?
21st May 2018, 9:56 AM
Lucas Knook
Lucas Knook - avatar
0
Am I correct if I say when the msb is 1, and the number is negative, I have to look for the zero's instead of the one's and then substract 1 from what I get? Also, how does the computer know it uses one's complement or two's complement? Or can a computer only use one of them?
21st May 2018, 10:13 AM
Lucas Knook
Lucas Knook - avatar
0
I know complementing 0 and 1 is its opposite. 1 become 0 and 0 become 1.
21st May 2018, 4:56 PM
Apple Blossom
Apple Blossom - avatar
0
So, I can say that when I use bitwise not, it flips all the ones and zero's including the MSB, which tells the computer that it is a negative number and then it reads 0 and 1 the opposite. after that it substracts one.
21st May 2018, 5:10 PM
Lucas Knook
Lucas Knook - avatar
0
Yeah
21st May 2018, 5:11 PM
Max
Max - avatar
0
I only have a little question left. Why does it substract one? can't 1111 1111 just be zero?
21st May 2018, 5:11 PM
Lucas Knook
Lucas Knook - avatar
0
Cause then you have two zeros 0000 0000 and 1111 1111
21st May 2018, 5:12 PM
Max
Max - avatar
0
ah, that makes sense. I understand now, thanks!
21st May 2018, 5:13 PM
Lucas Knook
Lucas Knook - avatar