Answer the question
In order to leave comments, you need to log in
How is a negative number represented in two's complement code?
1111 1111 = 256. (This is how the number 256
is represented in binary, as far as I know)
1111 1111 = -127
, what should be done?
Answer the question
In order to leave comments, you need to log in
> 1111 1111 = 256. (This is how the number 256 is represented in binary, as far as I know)
> 1111 1111 = -127 (In the reverse code (two's compliment) -127 looks exactly like this, judging by Wikipedia)
Wrong.
1111-1111 = 255
256 is:
0000-0001 0000-0000
The most significant bit indicates the sign, 0 - for zero and positive, 1 - for negative
to change the sign, you need to invert the binary notation of the number and add 1 to it,
for example, 127 in binary notation 0111 1111
invert - 1000 0000 and add one - 1000 0001 - there will be a number entry -127
again invert the resulting number - 0111 1110, add one - 0111 1111 - again got 127
you messed something up 1111 1111 is 255 in the unsigned binary system, and in the signed binary system it is -1 (however)
there are types for this in the compiler: signed and unsigned,
the unsigned type takes and stupidly grows as bits increase, unsigned is also only non-modulo and absolutely and in the binary system
1111 1111 is obviously greater than 1111 1110 the first is -1 and the second -2
when compiling the operation (let's say multiplication) is translated into machine code
in assembler it will look like this
mul EAX,EBX for unsigned and
imul EAX,EBX for signed like
this is how the processor distinguishes them
for addition, there are no differences, but embarrassment can happen during the output - when 127 is followed by -128 then -127 then -126
First, you are wrong. In an unsigned type 1111.1111 = 255 (not 256!), in a signed type 1111.1111 = -1 (not -127).
1000.0000 - respectively -128 and 128.
256 cannot be transferred in one byte - neither in the signed type, nor in the unsigned one.
Well, the answer to your main question.
If it is known that the cell contains a signed number - as mentioned above, by the upper bit (1 - minus, 0 - zero or plus). And to distinguish between a signed and an unsigned number is, in fact, not the work of a machine, but of a programmer and a compiler, and if a cell of an unknown type is simply given, you won’t know if it’s signed there, unsigned, fractional, pointer or symbol.
• Addition and subtraction work the same for signed and unsigned numbers.
• Overflow check and comparison. Three flags are used: the equal flag (zf, zero), the signed overflow flag (of, overflow), and the unsigned overflow flag (cf, carry). Each of the arithmetic functions fills both signed and unsigned flags, and there are separate branching functions for unsigned numbers (above/below - for example, ja, jbe = jna ...) and signed ones (greater/less - jg, jle = jng). Jump if above, jump if below or equal, etc.
• Multiplication and division are also different functions, unsigned mul/div and signed imul/idiv.
By the way, for this reason, the compiler swears if you need to compare signed and unsigned. The only way to reliably do this, for example, for 4-byte numbers, is to expand to 8-byte signed to fit both.
0111 1111 = 127. Please check.
It is the eighth bit that denotes the sign. 0 - plus, 1 - minus.
127 - 01111111 - direct code, positive number
-127 - 11111111 - reverse code
-127 - 10000001 - additional code
-128 - 10000000 - additional code
If 1 bit = 1, then the number is negative.
Read the wiki carefully.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question