Two's complement: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(5 intermediate revisions by 4 users not shown)
Line 91:
The two's complement of an integer 'n' can also be calculated by adding 1 to its bitwise complement '~n'.
<syntaxhighlight lang="c++">
#include <cstdint>
#include <iostream>
#include <iomanip>
Line 96 ⟶ 97:
#include <bitset>
 
std::string to_hex(const int32_t number) {
std::stringstream stream;
stream << std::setfill('0') << std::setw(8) << std::hex << number;
Line 102 ⟶ 103:
}
 
std::string to_binary(const int32_t number) {
std::stringstream stream;
stream << std::bitset<16>(number);
Line 108 ⟶ 109:
}
 
int32_t twos_complement(const int32_t number) {
return ~number + 1;
}
Line 127 ⟶ 128:
<< std::setw(20) << "----------------" << std::setw(20) << "----------------" << std::endl;
 
for ( const int32_t& example : examples ) {
std::cout << std::setw(6) << example << std::setw(17) << to_upper_case(to_hex(example))
<< std::setw(20) << to_binary(example) << std::setw(13) << twos_complement(example) << std::endl;
Line 157 ⟶ 158:
 
end</syntaxhighlight>
{{out| Output}}<pre>
-1234567 : 1234567
-1234566 : 1234566
-2 : 2
-1 : 1
0 : 0
1 : -1
2 : -2
1234565 : -1234565
1234566 : -1234566
</pre>
 
=={{header|Delphi}}==
Line 273 ⟶ 285:
for ( int example : examples ) {
System.out.println(String.format("%5d%18s%36s%13d",
String.format("%5d%18s%36s%13d", example, toHex(example), toBinary(example), twosComplement(example)));
}
}
Line 505 ⟶ 517:
By default Rakus integers are arbitrary sized, theoretically of infinite length. You can't really take the twos complement of an infinitely long number; so, we need to specifically use fixed size integers.
 
There is a module available from the Raku ecosystem that provides fixed size integer support, named (appropriately enough.) [https://raku.land/githubzef:thundergnat/FixedInt FixedInt].
 
FixedInt supports fixed bit size integers, not only 8 bit, 16 bit, 32 bit or 64 bit, but ''ANY'' integer size. 22 bit, 35 bit, 191 bit, whatever.
Line 541 ⟶ 553:
</pre>
=={{header|Ruby}}==
Ruby integers have has a built-in 'one-complement'method: '''~''', which flips all bits. Adding 1 leads to a negative integer:
<syntaxhighlight lang="ruby">~42 + 1 # => -42
</syntaxhighlight>
 
=={{header|Wren}}==
Strictly speaking, Wren doesn't have integers. Instead all numbers are 'IEEE 754' 64 bit floating point values (their underlying C type being ''double'') and negative numbers are therefore represented using the ''offset binary'' method rather than ''two's complement''.
 
This is illustrated by running the following code:
<syntaxhighlight lang="ecmascriptwren">var a = 0
a = -a
System.print(a) // -0</syntaxhighlight>
Line 557 ⟶ 570:
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:
 
<syntaxhighlight lang="ecmascriptwren">var pow32 = 2.pow(32)
var pow31 = 2.pow(31)
var bs = [-pow31, -pow31+1, -2, -1, 0, 1, 2, pow31-2, pow31-1]
9,476

edits