Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
28
Task/Twos-complement/8080-Assembly/twos-complement.8080
Normal file
28
Task/Twos-complement/8080-Assembly/twos-complement.8080
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
org 100h ; program load point under CP/M
|
||||
;
|
||||
; the assembler itself will negate an
|
||||
; 8 or 16-bit constant for us
|
||||
;
|
||||
mvi a,-43 ; a = D5
|
||||
lxi h,-43 ; hl = FFD5
|
||||
;
|
||||
; no built-in opcode to negate an 8-bit
|
||||
; register, but nevertheless easily done
|
||||
;
|
||||
mvi a,43 ; a = 2B
|
||||
cma
|
||||
inr a ; a = D5
|
||||
;
|
||||
; the same approach for 16-bit values
|
||||
;
|
||||
lxi h,43 ; hl = 002B
|
||||
mov a,l
|
||||
cma
|
||||
mov l,a
|
||||
mov a,h
|
||||
cma
|
||||
mov h,a
|
||||
inx h ; hl = FFD5
|
||||
;
|
||||
jmp 0 ; exit to CP/M
|
||||
end
|
||||
10
Task/Twos-complement/Ada/twos-complement.adb
Normal file
10
Task/Twos-complement/Ada/twos-complement.adb
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
with Ada.Text_IO;
|
||||
|
||||
procedure Twos_Complement is
|
||||
type Number is mod 2 ** 32;
|
||||
A : constant Number := 42;
|
||||
B : constant Number := not A + 1;
|
||||
Is_Negative : constant Boolean := A + B = 0;
|
||||
begin
|
||||
Ada.Text_IO.Put_Line ("B = -A is " & Is_Negative'Image);
|
||||
end Twos_Complement;
|
||||
|
|
@ -2,5 +2,5 @@ d = 1234567
|
|||
dim b = {-d, -d+1, -2, -1, 0, 1, 2, d-2, d-1}
|
||||
|
||||
for i = 0 to b[?]-1
|
||||
print b[i]; " -> "; -b[i]
|
||||
print b[i]; " -> "; -b[i]
|
||||
next i
|
||||
|
|
|
|||
|
|
@ -11,32 +11,32 @@ std::string to_hex(const int32_t number) {
|
|||
}
|
||||
|
||||
std::string to_binary(const int32_t number) {
|
||||
std::stringstream stream;
|
||||
stream << std::bitset<16>(number);
|
||||
return stream.str();
|
||||
std::stringstream stream;
|
||||
stream << std::bitset<16>(number);
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
int32_t twos_complement(const int32_t number) {
|
||||
return ~number + 1;
|
||||
return ~number + 1;
|
||||
}
|
||||
|
||||
std::string to_upper_case(std::string str) {
|
||||
for ( char& ch : str ) {
|
||||
ch = toupper(ch);
|
||||
}
|
||||
return str;
|
||||
for ( char& ch : str ) {
|
||||
ch = toupper(ch);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::vector<int32_t> examples = { 0, 1, -1, 42 };
|
||||
std::vector<int32_t> examples = { 0, 1, -1, 42 };
|
||||
|
||||
std::cout << std::setw(9) << "decimal" << std::setw(12) << "hex"
|
||||
<< std::setw(17) << "binary" << std::setw(25) << "two's complement" << std::endl;
|
||||
std::cout << std::setw(6) << "-----------" << std::setw(12) << "--------"
|
||||
<< std::setw(20) << "----------------" << std::setw(20) << "----------------" << std::endl;
|
||||
std::cout << std::setw(9) << "decimal" << std::setw(12) << "hex"
|
||||
<< std::setw(17) << "binary" << std::setw(25) << "two's complement" << std::endl;
|
||||
std::cout << std::setw(6) << "-----------" << std::setw(12) << "--------"
|
||||
<< 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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
31
Task/Twos-complement/COBOL/twos-complement.cob
Normal file
31
Task/Twos-complement/COBOL/twos-complement.cob
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. Twos_complement.
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 d PIC S9(7) COMP-5 VALUE 1234567.
|
||||
01 neg-d PIC S9(7) COMP-5.
|
||||
01 neg-d1 PIC S9(7) COMP-5.
|
||||
01 neg-b PIC S9(7) COMP-5.
|
||||
01 d2 PIC S9(7) COMP-5.
|
||||
01 d1 PIC S9(7) COMP-5.
|
||||
01 b OCCURS 9 TIMES PIC S9(7) COMP-5.
|
||||
01 i PIC 9(2) VALUE 1.
|
||||
PROCEDURE DIVISION.
|
||||
COMPUTE neg-d = -d
|
||||
COMPUTE neg-d1 = -d + 1
|
||||
COMPUTE d2 = d - 2
|
||||
COMPUTE d1 = d - 1
|
||||
MOVE neg-d TO b(1)
|
||||
MOVE neg-d1 TO b(2)
|
||||
MOVE -2 TO b(3)
|
||||
MOVE -1 TO b(4)
|
||||
MOVE 0 TO b(5)
|
||||
MOVE 1 TO b(6)
|
||||
MOVE 2 TO b(7)
|
||||
MOVE d2 TO b(8)
|
||||
MOVE d1 TO b(9)
|
||||
PERFORM VARYING i FROM 1 BY 1 UNTIL i > 9
|
||||
COMPUTE neg-b = -b(i)
|
||||
DISPLAY b(i) " -> " neg-b
|
||||
END-PERFORM
|
||||
STOP RUN.
|
||||
|
|
@ -6,7 +6,7 @@ arraysize s, b
|
|||
|
||||
for i = 0 to s - 1
|
||||
|
||||
print b[i], " : ", b[i] * -1
|
||||
print b[i], " : ", b[i] * -1
|
||||
|
||||
next i
|
||||
|
||||
|
|
|
|||
1
Task/Twos-complement/Crystal/twos-complement.cr
Normal file
1
Task/Twos-complement/Crystal/twos-complement.cr
Normal file
|
|
@ -0,0 +1 @@
|
|||
~3 + 1 # => -3
|
||||
|
|
@ -2,29 +2,29 @@ import java.util.List;
|
|||
|
||||
public final class TwosComplement {
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<Integer> examples = List.of( 0, 1, -1, 42 );
|
||||
|
||||
System.out.println(String.format("%9s%12s%24s%34s", "decimal", "hex", "binary", "two's complement"));
|
||||
System.out.println(
|
||||
public static void main(String[] args) {
|
||||
List<Integer> examples = List.of( 0, 1, -1, 42 );
|
||||
|
||||
System.out.println(String.format("%9s%12s%24s%34s", "decimal", "hex", "binary", "two's complement"));
|
||||
System.out.println(
|
||||
String.format("%6s%12s%24s%32s", "-----------", "--------", "----------", "----------------"));
|
||||
|
||||
for ( int example : examples ) {
|
||||
System.out.println(String.format("%5d%18s%36s%13d",
|
||||
|
||||
for ( int example : examples ) {
|
||||
System.out.println(String.format("%5d%18s%36s%13d",
|
||||
example, toHex(example), toBinary(example), twosComplement(example)));
|
||||
}
|
||||
}
|
||||
|
||||
private static String toHex(int number) {
|
||||
return String.format("%8s", Integer.toHexString(number).toUpperCase()).replace(" ", "0");
|
||||
}
|
||||
|
||||
private static String toBinary(int number) {
|
||||
return String.format("%32s", Integer.toBinaryString(number)).replace(" ", "0");
|
||||
}
|
||||
|
||||
private static int twosComplement(int number) {
|
||||
return ~number + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String toHex(int number) {
|
||||
return String.format("%8s", Integer.toHexString(number).toUpperCase()).replace(" ", "0");
|
||||
}
|
||||
|
||||
private static String toBinary(int number) {
|
||||
return String.format("%32s", Integer.toBinaryString(number)).replace(" ", "0");
|
||||
}
|
||||
|
||||
private static int twosComplement(int number) {
|
||||
return ~number + 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
2
Task/Twos-complement/JavaScript/twos-complement.js
Normal file
2
Task/Twos-complement/JavaScript/twos-complement.js
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
console.log([0, 1, -1, 42, 10 ** 11].map(x => ~x + 1).join(" "));
|
||||
console.log(~BigInt(10 ** 11) + 1n);
|
||||
|
|
@ -1,12 +1,12 @@
|
|||
Module Complement2{
|
||||
// we use binary.and to get a number in range of byte 0 to 255
|
||||
byte k, v
|
||||
v=random(1, 255) ' there is no two's complement for zero
|
||||
z=binary.and(binary.not(v)+1, 0xFF)
|
||||
print v
|
||||
print z
|
||||
print z=255-v+1 // z is type of byte always positive
|
||||
print sint(z+0xFFFFFF00)=-v // using 4bytes, we add unsinged 0xFFFFFF00
|
||||
// we use binary.and to get a number in range of byte 0 to 255
|
||||
byte k, v
|
||||
v=random(1, 255) ' there is no two's complement for zero
|
||||
z=binary.and(binary.not(v)+1, 0xFF)
|
||||
print v
|
||||
print z
|
||||
print z=255-v+1 // z is type of byte always positive
|
||||
print sint(z+0xFFFFFF00)=-v // using 4bytes, we add unsinged 0xFFFFFF00
|
||||
}
|
||||
Complement2
|
||||
Complement2
|
||||
|
|
|
|||
8
Task/Twos-complement/Pluto/twos-complement.pluto
Normal file
8
Task/Twos-complement/Pluto/twos-complement.pluto
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
local fmt = require "fmt"
|
||||
|
||||
local decimal = {0, 1, -1, 42}
|
||||
print("decimal two's complement")
|
||||
print("------- ----------------")
|
||||
for decimal as d do
|
||||
fmt.print(" %2d %3d", d, ~d + 1)
|
||||
end
|
||||
9
Task/Twos-complement/Rebol/twos-complement.rebol
Normal file
9
Task/Twos-complement/Rebol/twos-complement.rebol
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
negate 0 ;== 0
|
||||
negate -1 ;== 1
|
||||
negate 1:0:0 ;== -1:0:0
|
||||
negate -1x2 ;== 1x-2
|
||||
negate $100 ;== -$100
|
||||
;; or
|
||||
1 + complement -42 ;== 42
|
||||
complement true ;== false
|
||||
complement 2#{0000 0011} ;== 2#{1111 1100}
|
||||
5
Task/Twos-complement/V-(Vlang)/twos-complement.v
Normal file
5
Task/Twos-complement/V-(Vlang)/twos-complement.v
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
fn main() {
|
||||
mut x := 56
|
||||
x = ~x + 1
|
||||
println(x)
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
PROGRAM "Two's complement"
|
||||
VERSION "0.0000"
|
||||
PROGRAM "Two's complement"
|
||||
VERSION "0.0000"
|
||||
|
||||
DECLARE FUNCTION Entry ()
|
||||
|
||||
|
|
|
|||
|
|
@ -12,5 +12,5 @@ b(7) = d-2
|
|||
b(8) = d-1
|
||||
|
||||
for i = 0 to arraysize(b(), 1)
|
||||
print b(i), " -> ", -b(i)
|
||||
print b(i), " -> ", -b(i)
|
||||
next i
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue