Two's complement: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 13:
=={{header|6502 Assembly}}==
===8-Bit===
<langsyntaxhighlight lang="6502asm">LDA #%01010101
EOR #255
CLC
ADC #1 ;result: #%10101011</langsyntaxhighlight>
===16-bit===
<langsyntaxhighlight lang="6502asm">myVar equ $20
 
LDA #3
Line 39:
LDA myVar+1
ADC #0
STA myVar+1</langsyntaxhighlight>
 
=={{header|68000 Assembly}}==
<langsyntaxhighlight lang="68000devpac">MOVE.L #3,D0
NEG.L D0 ;D0 = #$FFFFFFFD</langsyntaxhighlight>
 
=={{header|8086 Assembly}}==
<langsyntaxhighlight lang="asm">mov al,17
neg al ;8-bit
mov bx,4C00h
neg bx ;16-bit</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight lang="algol68">BEGIN
INT a := 3;
print( ( -a, " ", 1 + ABS NOT BIN a, newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 66:
=={{header|ALGOL W}}==
{{Trans|ALGOL 68}}
<langsyntaxhighlight lang="algolw">begin
integer a;
a := 3;
write( i_w := 1, s_w := 1, -a, 1 + number( not bitstring( a ) ) )
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 77:
 
=={{header|ARM Assembly}}==
<langsyntaxhighlight ARMlang="arm Assemblyassembly">MOV R0,#0x0000000F
MOV R1,#1
MVN R0,R0 ;flips the bits of R0, R0 = 0xFFFFFFF0
ADD R0,R0,R1 ;R0 = 0xFFFFFFF1</langsyntaxhighlight>
 
=={{header|C}}==
<langsyntaxhighlight Clang="c">int a = 3;
a = -a;</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight lang="freebasic">Dim As Integer d1 = 2147483648, d2 = 2147483646
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</langsyntaxhighlight>
{{out}}
<pre>0000000000000011 -> 1111111111111101</pre>
 
=== inline assembly ===
<langsyntaxhighlight lang="freebasic">Dim As Integer a = &b000011
Dim As Integer a2c, l
#ifdef __FB_64BIT__
Line 117:
 
Print Bin(a, l); " -> "; Bin(a2c, l)
Sleep</langsyntaxhighlight>
{{out}}
<pre>-2147483648 -> 2147483648
Line 131:
=={{header|J}}==
J uses twos complement natively:
<langsyntaxhighlight Jlang="j"> -3
_3</langsyntaxhighlight>
 
We can see this by extracting bits representing the number. In this example, we limit ourselves to 8 bits:
 
<langsyntaxhighlight Jlang="j"> (8#2)#:3
0 0 0 0 0 0 1 1
(8#2)#:-3
1 1 1 1 1 1 0 1</langsyntaxhighlight>
 
=={{header|Julia}}==
Line 146:
=={{header|Phix}}==
=== inline assembly ===
<!--<langsyntaxhighlight Phixlang="phix">-->
<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 -&gt; %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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 167:
</pre>
=== normal hll ===
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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 -&gt; %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>
<!--</langsyntaxhighlight>-->
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>.
<langsyntaxhighlight lang="pli">100H: /* TWO'S COMPLEMENT *?
 
/* CP/M BDOS SYSTEM CALL */
Line 203:
CALL PR$NL;
 
EOF</langsyntaxhighlight>
{{out}}
<pre>
Line 218:
Here we'll demonstrate twos complement on a 57 bit integer.
 
<syntaxhighlight lang="raku" perl6line>use FixedInt;
 
# 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</langsyntaxhighlight>
{{out}}
<pre>144114427045277101
Line 242:
 
This is illustrated by running the following code:
<langsyntaxhighlight lang="ecmascript">var a = 0
a = -a
System.print(a) // -0</langsyntaxhighlight>
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:
 
<langsyntaxhighlight lang="ecmascript">var pow32 = 2.pow(32)
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)")
}</langsyntaxhighlight>
 
{{out}}
Line 274:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">int I; char C;
[I:= 123;
I:= (~I) + 1;
Line 281:
C:= ~(C-1);
IntOut(0, C); CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
Line 294:
'''Zilog Z80'''
 
<langsyntaxhighlight lang="z80">ld a,%00001111
neg ;returns %11110001 in a</langsyntaxhighlight>
 
'''Game Boy'''
<langsyntaxhighlight lang="z80">ld a,%00001111
cpl ;game boy doesn't have NEG but it has CPL which flips all the bits.
inc a ;returns %11110001 in a</langsyntaxhighlight>
 
===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>.
<langsyntaxhighlight lang="z80">xor a ;ld a,0
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</langsyntaxhighlight>
10,327

edits