Two's complement: Difference between revisions
m
syntax highlighting fixup automation
Puppydrum64 (talk | contribs) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 13:
=={{header|6502 Assembly}}==
===8-Bit===
<
EOR #255
CLC
ADC #1 ;result: #%10101011</
===16-bit===
<
LDA #3
Line 39:
LDA myVar+1
ADC #0
STA myVar+1</
=={{header|68000 Assembly}}==
<
NEG.L D0 ;D0 = #$FFFFFFFD</
=={{header|8086 Assembly}}==
<
neg al ;8-bit
mov bx,4C00h
neg bx ;16-bit</
=={{header|ALGOL 68}}==
Line 55:
<br>
Note: BIN a converts a to a BITS (bit-string) value, the NOT operator will flip the bits and the ABS operator will convert back to an integer, so <code>1 + ABS NOT BIN a</code> is a long-winded alternative to <code>-a</code>. Note in Algol 68, the BIN operator cannot be applied to negative integers, so <code>1 + ABS NOT BIN -3</code> won't work.
<
INT a := 3;
print( ( -a, " ", 1 + ABS NOT BIN a, newline ) )
END</
{{out}}
<pre>
Line 66:
=={{header|ALGOL W}}==
{{Trans|ALGOL 68}}
<
integer a;
a := 3;
write( i_w := 1, s_w := 1, -a, 1 + number( not bitstring( a ) ) )
end.</
{{out}}
<pre>
Line 77:
=={{header|ARM Assembly}}==
<
MOV R1,#1
MVN R0,R0 ;flips the bits of R0, R0 = 0xFFFFFFF0
ADD R0,R0,R1 ;R0 = 0xFFFFFFF1</
=={{header|C}}==
<
a = -a;</
=={{header|FreeBASIC}}==
In FreeBASIC as in C, if a number n is any integer type, then -n is the two's complement of n, with type preserved.
<
Dim As Integer b(1 To ...) = {-d1, -d1+1, -2, -1, 0, 1, 2, d1-2, d1-1}
For i As Integer = 1 To Ubound(b)
Print b(i); " -> "; -b(i)
Next i
Sleep</
{{out}}
<pre>0000000000000011 -> 1111111111111101</pre>
=== inline assembly ===
<
Dim As Integer a2c, l
#ifdef __FB_64BIT__
Line 117:
Print Bin(a, l); " -> "; Bin(a2c, l)
Sleep</
{{out}}
<pre>-2147483648 -> 2147483648
Line 131:
=={{header|J}}==
J uses twos complement natively:
<
_3</
We can see this by extracting bits representing the number. In this example, we limit ourselves to 8 bits:
<
0 0 0 0 0 0 1 1
(8#2)#:-3
1 1 1 1 1 1 0 1</
=={{header|Julia}}==
Line 146:
=={{header|Phix}}==
=== inline assembly ===
<!--<
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0b000011</span><span style="color: #0000FF;">,</span>
Line 161:
}
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%032b -> %032b\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a2c</span><span style="color: #0000FF;">})</span>
<!--</
{{out}}
<pre>
Line 167:
</pre>
=== normal hll ===
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0b000011</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%032b -> %032b\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">a</span><span style="color: #0000FF;">})</span>
<!--</
Same output (naturally the rhs is twice as long under 64 bit, in both cases)
Line 178:
<br>
Even though the original PL/M 8080 compiler only handles unsigned integers, <code>-A</code> two's complements <code>A</code>.
<
/* CP/M BDOS SYSTEM CALL */
Line 203:
CALL PR$NL;
EOF</
{{out}}
<pre>
Line 218:
Here we'll demonstrate twos complement on a 57 bit integer.
<syntaxhighlight lang="raku"
# Instantiate a new 57(!) bit fixed size integer
Line 231:
say fixedint; # Echo the value to the console in decimal format
say fixedint.bin; # Echo the value to the console in binary format</
{{out}}
<pre>144114427045277101
Line 242:
This is illustrated by running the following code:
<
a = -a
System.print(a) // -0</
which produces 'negative zero' rather than just 'zero'.
Line 251:
We can therefore emulate how two's complement works on ''signed'' 32 bit integers by using the bitwise complement operator '''~''' to flip the bits as follows:
<
var pow31 = 2.pow(31)
var bs = [-pow31, -pow31+1, -2, -1, 0, 1, 2, pow31-2, pow31-1]
Line 258:
if (b2 > pow31) b2 = b2 - pow32
System.print("%(b) -> %(b2)")
}</
{{out}}
Line 274:
=={{header|XPL0}}==
<
[I:= 123;
I:= (~I) + 1;
Line 281:
C:= ~(C-1);
IntOut(0, C); CrLf(0);
]</
{{out}}
Line 294:
'''Zilog Z80'''
<
neg ;returns %11110001 in a</
'''Game Boy'''
<
cpl ;game boy doesn't have NEG but it has CPL which flips all the bits.
inc a ;returns %11110001 in a</
===16 Bit===
<code>NEG</code> and <code>CPL</code> only work on the accumulator <code>A</code>.
The following can be written to work with <code>BC</code>, <code>DE</code>, <code>HL</code>, <code>IX</code>, or <code>IY</code>.
<
sub c
ld c,a
sbc a ;loads &FF into A if "sub c" set the carry (borrow) flag. Otherwise, a remains zero.
sub b
ld b,a</
|