Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
5
Task/Twos-complement/00-META.yaml
Normal file
5
Task/Twos-complement/00-META.yaml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
category:
|
||||
- Simple
|
||||
from: http://rosettacode.org/wiki/Two's_complement
|
||||
note: Basic language learning
|
||||
9
Task/Twos-complement/00-TASK.txt
Normal file
9
Task/Twos-complement/00-TASK.txt
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
[[wp:Two's_complement|Two's complement]] is an important concept in representing negative numbers. To turn a positive integer negative, flip the bits and add one.
|
||||
|
||||
|
||||
|
||||
;Task
|
||||
Show how to calculate the two's complement of an integer. (It doesn't necessarily need to be a 32 bit integer.)
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
LDA #%01010101
|
||||
EOR #255
|
||||
CLC
|
||||
ADC #1 ;result: #%10101011
|
||||
22
Task/Twos-complement/6502-Assembly/twos-complement-2.6502
Normal file
22
Task/Twos-complement/6502-Assembly/twos-complement-2.6502
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
myVar equ $20
|
||||
|
||||
LDA #3
|
||||
STA myVar
|
||||
LDA #0
|
||||
STA myVar+1 ;equivalent C: uint16_t myVar = 3;
|
||||
|
||||
negate:
|
||||
LDA myVar+1
|
||||
EOR #255
|
||||
STA myVar+1
|
||||
|
||||
LDA myVar
|
||||
EOR #255
|
||||
STA myVar
|
||||
CLC
|
||||
ADC #1
|
||||
STA myVar
|
||||
;this handles the case if we started with something where the low byte was zero.
|
||||
LDA myVar+1
|
||||
ADC #0
|
||||
STA myVar+1
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
MOVE.L #3,D0
|
||||
NEG.L D0 ;D0 = #$FFFFFFFD
|
||||
4
Task/Twos-complement/8086-Assembly/twos-complement.8086
Normal file
4
Task/Twos-complement/8086-Assembly/twos-complement.8086
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
mov al,17
|
||||
neg al ;8-bit
|
||||
mov bx,4C00h
|
||||
neg bx ;16-bit
|
||||
4
Task/Twos-complement/ALGOL-68/twos-complement.alg
Normal file
4
Task/Twos-complement/ALGOL-68/twos-complement.alg
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
BEGIN
|
||||
INT a := 3;
|
||||
print( ( -a, " ", 1 + ABS NOT BIN a, newline ) )
|
||||
END
|
||||
5
Task/Twos-complement/ALGOL-W/twos-complement.alg
Normal file
5
Task/Twos-complement/ALGOL-W/twos-complement.alg
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
begin
|
||||
integer a;
|
||||
a := 3;
|
||||
write( i_w := 1, s_w := 1, -a, 1 + number( not bitstring( a ) ) )
|
||||
end.
|
||||
4
Task/Twos-complement/ARM-Assembly/twos-complement.arm
Normal file
4
Task/Twos-complement/ARM-Assembly/twos-complement.arm
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
MOV R0,#0x0000000F
|
||||
MOV R1,#1
|
||||
MVN R0,R0 ;flips the bits of R0, R0 = 0xFFFFFFF0
|
||||
ADD R0,R0,R1 ;R0 = 0xFFFFFFF1
|
||||
41
Task/Twos-complement/C++/twos-complement.cpp
Normal file
41
Task/Twos-complement/C++/twos-complement.cpp
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <vector>
|
||||
#include <bitset>
|
||||
|
||||
std::string to_hex(int32_t number) {
|
||||
std::stringstream stream;
|
||||
stream << std::setfill('0') << std::setw(8) << std::hex << number;
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
std::string to_binary(int32_t number) {
|
||||
std::stringstream stream;
|
||||
stream << std::bitset<16>(number);
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
int32_t twos_complement(int32_t number) {
|
||||
return ~number + 1;
|
||||
}
|
||||
|
||||
std::string to_upper_case(std::string str) {
|
||||
for ( char& ch : str ) {
|
||||
ch = toupper(ch);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
int main() {
|
||||
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;
|
||||
|
||||
for ( 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;
|
||||
}
|
||||
}
|
||||
2
Task/Twos-complement/C/twos-complement.c
Normal file
2
Task/Twos-complement/C/twos-complement.c
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
int a = 3;
|
||||
a = -a;
|
||||
13
Task/Twos-complement/Craft-Basic/twos-complement.basic
Normal file
13
Task/Twos-complement/Craft-Basic/twos-complement.basic
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
let d = 1234567
|
||||
|
||||
dim b[d * -1, d * -1 + 1, -2, -1, 0, 1, 2, d - 2, d - 1]
|
||||
|
||||
arraysize s, b
|
||||
|
||||
for i = 0 to s - 1
|
||||
|
||||
print b[i], " : ", b[i] * -1
|
||||
|
||||
next i
|
||||
|
||||
end
|
||||
11
Task/Twos-complement/Delphi/twos-complement.delphi
Normal file
11
Task/Twos-complement/Delphi/twos-complement.delphi
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
procedure TwosCompliment(Memo: TMemo);
|
||||
var N: integer;
|
||||
begin
|
||||
N:=123456789;
|
||||
Memo.Lines.Add(Format('N=%10d $%0.8x',[N,N]));
|
||||
Memo.Lines.Add('');
|
||||
Memo.Lines.Add('N:=(N xor $FFFFFFFF)+1');
|
||||
N:=(N xor $FFFFFFFF)+1;
|
||||
Memo.Lines.Add('');
|
||||
Memo.Lines.Add(Format('N=%10d $%0.8x',[N,N]));
|
||||
end;
|
||||
2
Task/Twos-complement/Forth/twos-complement.fth
Normal file
2
Task/Twos-complement/Forth/twos-complement.fth
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
: 2s'complement base @ swap dup negate swap 2 base ! u. u. base ! ;
|
||||
25 2s'complement
|
||||
6
Task/Twos-complement/FreeBASIC/twos-complement-1.basic
Normal file
6
Task/Twos-complement/FreeBASIC/twos-complement-1.basic
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
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
|
||||
20
Task/Twos-complement/FreeBASIC/twos-complement-2.basic
Normal file
20
Task/Twos-complement/FreeBASIC/twos-complement-2.basic
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
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
|
||||
2
Task/Twos-complement/J/twos-complement-1.j
Normal file
2
Task/Twos-complement/J/twos-complement-1.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
-3
|
||||
_3
|
||||
4
Task/Twos-complement/J/twos-complement-2.j
Normal file
4
Task/Twos-complement/J/twos-complement-2.j
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(8#2)#:3
|
||||
0 0 0 0 0 0 1 1
|
||||
(8#2)#:-3
|
||||
1 1 1 1 1 1 0 1
|
||||
6
Task/Twos-complement/Jakt/twos-complement.jakt
Normal file
6
Task/Twos-complement/Jakt/twos-complement.jakt
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
fn main() {
|
||||
let n = 0xabcdeabcdeu64
|
||||
println("{:064b}", n)
|
||||
println("{:064b}", ~n + 1)
|
||||
println("{:064b}", -n) // Same result
|
||||
}
|
||||
30
Task/Twos-complement/Java/twos-complement.java
Normal file
30
Task/Twos-complement/Java/twos-complement.java
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
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(
|
||||
String.format("%6s%12s%24s%32s", "-----------", "--------", "----------", "----------------"));
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
12
Task/Twos-complement/M2000-Interpreter/twos-complement.m2000
Normal file
12
Task/Twos-complement/M2000-Interpreter/twos-complement.m2000
Normal file
|
|
@ -0,0 +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
|
||||
}
|
||||
Complement2
|
||||
Complement2
|
||||
11
Task/Twos-complement/Nim/twos-complement.nim
Normal file
11
Task/Twos-complement/Nim/twos-complement.nim
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import std/[strformat, strutils]
|
||||
|
||||
func twosComplement[T: SomeSignedInt](n: T): T =
|
||||
## Compute the two's complement of "n".
|
||||
not n + 1
|
||||
|
||||
echo &"""{"n":^15}{"2's complement":^15}{"-n":^15}"""
|
||||
for n in [0i32, 1i32, -1i32]:
|
||||
echo &"{n.toHex:^15}{twosComplement(n).toHex:^15}{(-n).toHex:^15}"
|
||||
for n in [-50i8, 50i8]:
|
||||
echo &"{n.toHex:^15}{twosComplement(n).toHex:^15}{(-n).toHex:^15}"
|
||||
26
Task/Twos-complement/PL-M/twos-complement.plm
Normal file
26
Task/Twos-complement/PL-M/twos-complement.plm
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
100H: /* TWO'S COMPLEMENT *?
|
||||
|
||||
/* CP/M BDOS SYSTEM CALL */
|
||||
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5;END;
|
||||
/* CONSOLE OUTPUT ROUTINES */
|
||||
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
|
||||
PR$NL: PROCEDURE; CALL PR$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END;
|
||||
PR$HEX: PROCEDURE( B ); /* PRINTS B AS A 2 DIGIT HEX NUMBER */
|
||||
DECLARE B BYTE;
|
||||
DECLARE D BYTE;
|
||||
IF ( D := SHR( B, 4 ) ) > 9 THEN CALL PR$CHAR( ( D - 10 ) + 'A' );
|
||||
ELSE CALL PR$CHAR( D + '0' );
|
||||
IF ( D := B AND 0FH ) > 9 THEN CALL PR$CHAR( ( D - 10 ) + 'A' );
|
||||
ELSE CALL PR$CHAR( D + '0' );
|
||||
END PR$HEX ;
|
||||
|
||||
DECLARE A BYTE;
|
||||
|
||||
A = 1;
|
||||
CALL PR$HEX( A );
|
||||
CALL PR$CHAR( ' ' );
|
||||
A = -A;
|
||||
CALL PR$HEX( A );
|
||||
CALL PR$NL;
|
||||
|
||||
EOF
|
||||
6
Task/Twos-complement/Perl/twos-complement.pl
Normal file
6
Task/Twos-complement/Perl/twos-complement.pl
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
|
||||
for ( -2**31, -2**31+1, -2, -1, 0, 1, 2, 2**31-2, 2**31-1 ) {
|
||||
printf "$_ -> %d\n", $_ == 0 ? 0 : ~$_+1
|
||||
}
|
||||
16
Task/Twos-complement/Phix/twos-complement-1.phix
Normal file
16
Task/Twos-complement/Phix/twos-complement-1.phix
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
-->
|
||||
<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>
|
||||
<span style="color: #000000;">a2c</span>
|
||||
#ilASM{
|
||||
[32]
|
||||
mov eax,[a]
|
||||
neg eax
|
||||
mov [a2c],eax
|
||||
[64]
|
||||
mov rax,[a]
|
||||
neg rax
|
||||
mov [a2c],rax
|
||||
}
|
||||
<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 -> %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>
|
||||
<!--
|
||||
5
Task/Twos-complement/Phix/twos-complement-2.phix
Normal file
5
Task/Twos-complement/Phix/twos-complement-2.phix
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(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 -> %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>
|
||||
<!--
|
||||
1
Task/Twos-complement/Python/twos-complement-1.py
Normal file
1
Task/Twos-complement/Python/twos-complement-1.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
-n
|
||||
1
Task/Twos-complement/Python/twos-complement-2.py
Normal file
1
Task/Twos-complement/Python/twos-complement-2.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
~n+1
|
||||
14
Task/Twos-complement/Raku/twos-complement.raku
Normal file
14
Task/Twos-complement/Raku/twos-complement.raku
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
use FixedInt;
|
||||
|
||||
# Instantiate a new 57(!) bit fixed size integer
|
||||
my \fixedint = FixedInt.new: :57bits;
|
||||
|
||||
fixedint = (2³⁷ / 72 - 5¹⁷); # Set it to a large value
|
||||
|
||||
say fixedint; # Echo the value to the console in decimal format
|
||||
say fixedint.bin; # Echo the value to the console in binary format
|
||||
|
||||
fixedint.=C2; # Take the twos complement
|
||||
|
||||
say fixedint; # Echo the value to the console in decimal format
|
||||
say fixedint.bin; # Echo the value to the console in binary format
|
||||
1
Task/Twos-complement/Ruby/twos-complement.rb
Normal file
1
Task/Twos-complement/Ruby/twos-complement.rb
Normal file
|
|
@ -0,0 +1 @@
|
|||
~42 + 1 # => -42
|
||||
3
Task/Twos-complement/Wren/twos-complement-1.wren
Normal file
3
Task/Twos-complement/Wren/twos-complement-1.wren
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
var a = 0
|
||||
a = -a
|
||||
System.print(a) // -0
|
||||
8
Task/Twos-complement/Wren/twos-complement-2.wren
Normal file
8
Task/Twos-complement/Wren/twos-complement-2.wren
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
var pow32 = 2.pow(32)
|
||||
var pow31 = 2.pow(31)
|
||||
var bs = [-pow31, -pow31+1, -2, -1, 0, 1, 2, pow31-2, pow31-1]
|
||||
for (b in bs) {
|
||||
var b2 = ~b + 1
|
||||
if (b2 > pow31) b2 = b2 - pow32
|
||||
System.print("%(b) -> %(b2)")
|
||||
}
|
||||
8
Task/Twos-complement/XPL0/twos-complement.xpl0
Normal file
8
Task/Twos-complement/XPL0/twos-complement.xpl0
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
int I; char C;
|
||||
[I:= 123;
|
||||
I:= (~I) + 1;
|
||||
IntOut(0, I); CrLf(0);
|
||||
C:= -123;
|
||||
C:= ~(C-1);
|
||||
IntOut(0, C); CrLf(0);
|
||||
]
|
||||
2
Task/Twos-complement/Z80-Assembly/twos-complement-1.z80
Normal file
2
Task/Twos-complement/Z80-Assembly/twos-complement-1.z80
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
ld a,%00001111
|
||||
neg ;returns %11110001 in a
|
||||
3
Task/Twos-complement/Z80-Assembly/twos-complement-2.z80
Normal file
3
Task/Twos-complement/Z80-Assembly/twos-complement-2.z80
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
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
|
||||
6
Task/Twos-complement/Z80-Assembly/twos-complement-3.z80
Normal file
6
Task/Twos-complement/Z80-Assembly/twos-complement-3.z80
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue