Two's complement: Difference between revisions
Content deleted Content added
Added XPL0 example. |
Two's complement in FreeBASIC |
||
Line 76: | Line 76: | ||
<lang C>int a = 3; |
<lang C>int a = 3; |
||
a = -a;</lang> |
a = -a;</lang> |
||
=={{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. |
|||
<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</lang> |
|||
{{out}} |
|||
<pre>0000000000000011 -> 1111111111111101</pre> |
|||
=== inline assembly === |
|||
<lang freebasic>Dim As Integer a = &b000011 |
|||
Dim As Integer a2c, l |
|||
#ifdef __FB_64BIT__ |
|||
l = 16 |
|||
Asm |
|||
mov rax, [a] |
|||
neg rax |
|||
mov [a2c], rax |
|||
End Asm |
|||
#else |
|||
l = 8 |
|||
Asm |
|||
mov eax, [a] |
|||
neg eax |
|||
mov [a2c], eax |
|||
End Asm |
|||
#endif |
|||
Print Bin(a, l); " -> "; Bin(a2c, l) |
|||
Sleep</lang> |
|||
{{out}} |
|||
<pre>-2147483648 -> 2147483648 |
|||
-2147483647 -> 2147483647 |
|||
-2 -> 2 |
|||
-1 -> 1 |
|||
0 -> 0 |
|||
1 -> -1 |
|||
2 -> -2 |
|||
2147483646 -> -2147483646 |
|||
2147483647 -> -2147483647</pre> |
|||
=={{header|J}}== |
=={{header|J}}== |