I
I
Ivan the Terrible2015-10-11 16:14:23
Java
Ivan the Terrible, 2015-10-11 16:14:23

How to convert decimal negative number to binary?

How to implement it?
Everything is clear with positive ones, but I don’t know what to do with negative ones.
I could only get the reverse code of the number, but the last step, addition 1, led me to a dead end.
I saw implementations in PASCAL and C, I need in JAVA

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
Ivanq, 2015-10-11
@Ivanq

Given: -128
1. Make a negative number positive
-128 => 128 (10000000)
2. Negate all bits
128 (10000000) => 127 (01111111)
3. Add 1 to the resulting number
127 (01111111) => -128 (10000000 )

C
Curly Brace, 2015-10-11
@stasuss

and what is there? you invert all bits and you add unit to this number. this is how "two compliment" works

D
Daniil Smirnov, 2015-10-11
@antonsosnitzkij

I did not quite understand the question, because the solution itself is contained in it.
I will make an assumption that it is not clear - why is one added to the inverted value?

V
Vladimir Martyanov, 2015-10-11
@vilgeforce

Need to write. What exactly is the problem is not clear.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question