Two's complement: Difference between revisions

Added Algol 68
m (julia example)
(Added Algol 68)
Line 47:
neg bx ;16-bit</lang>
 
 
=={{header|ALGOL 68}}==
Algol 68 uses whatever representation the hardware the program is running on uses, which is almost certainly two's complement. So, as in C and most other languages, <code>-a</code> two's complements <code>a</code>. Using Algol 68's bit manipulation facilities, we can explicitely two's complement a positive integer, as shown in this example.
<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.
<lang algol68>BEGIN
INT a := 3;
print( ( -a, " ", 1 + ABS NOT BIN a, newline ) )
END</lang>
{{out}}
<pre>
-3 -3
</pre>
 
=={{header|C}}==
3,032

edits