Data update

This commit is contained in:
Ingy döt Net 2026-04-30 12:34:36 -04:00
parent 4bb20c9b71
commit cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions

View file

@ -0,0 +1,23 @@
with Ada.Text_IO, Interfaces;
use Ada.Text_IO, Interfaces;
procedure Bitwise is
subtype Byte is Unsigned_8;
package Byte_IO is new Ada.Text_Io.Modular_IO (Byte);
A : constant Byte := 2#00011110#;
B : constant Byte := 2#11110100#;
X : constant Byte := 128;
N : constant Natural := 1;
begin
Put ("A and B = "); Byte_IO.Put (Item => A and B, Base => 2); New_Line;
Put ("A or B = "); Byte_IO.Put (Item => A or B, Base => 2); New_Line;
Put ("A xor B = "); Byte_IO.Put (Item => A xor B, Base => 2); New_Line;
Put ("not A = "); Byte_IO.Put (Item => not A, Base => 2); New_Line;
New_Line (2);
Put_Line (Unsigned_8'Image (Shift_Left (X, N)));
Put_Line (Unsigned_8'Image (Shift_Right (X, N)));
Put_Line (Unsigned_8'Image (Shift_Right_Arithmetic (X, N)));
Put_Line (Unsigned_8'Image (Rotate_Left (X, N)));
Put_Line (Unsigned_8'Image (Rotate_Right (X, N)));
end Bitwise;

View file

@ -0,0 +1,12 @@
bitwise(255, 5)
Func bitwise($a, $b)
MsgBox(1, '', _
$a & " AND " & $b & ": " & BitAND($a, $b) & @CRLF & _
$a & " OR " & $b & ": " & BitOR($a, $b) & @CRLF & _
$a & " XOR " & $b & ": " & BitXOR($a, $b) & @CRLF & _
"NOT " & $a & ": " & BitNOT($a) & @CRLF & _
$a & " SHL " & $b & ": " & BitShift($a, $b * -1) & @CRLF & _
$a & " SHR " & $b & ": " & BitShift($a, $b) & @CRLF & _
$a & " ROL " & $b & ": " & BitRotate($a, $b) & @CRLF & _
$a & " ROR " & $b & ": " & BitRotate($a, $b * -1) & @CRLF )
EndFunc

View file

@ -0,0 +1,55 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. bitwise-ops.
DATA DIVISION.
LOCAL-STORAGE SECTION.
01 a PIC 1(32) USAGE BIT.
01 b PIC 1(32) USAGE BIT.
01 result PIC 1(32) USAGE BIT.
01 result-disp REDEFINES result
PIC S9(9) USAGE COMPUTATIONAL.
LINKAGE SECTION.
01 a-int USAGE BINARY-LONG.
01 b-int USAGE BINARY-LONG.
PROCEDURE DIVISION USING a-int, b-int.
MOVE FUNCTION BOOLEAN-OF-INTEGER(a-int, 32) TO a
MOVE FUNCTION BOOLEAN-OF-INTEGER(b-int, 32) TO b
COMPUTE result = a B-AND b
DISPLAY "a and b is " result-disp
COMPUTE result = a B-OR b
DISPLAY "a or b is " result-disp
COMPUTE result = B-NOT a
DISPLAY "Not a is " result-disp
COMPUTE result = a B-XOR b
DISPLAY "a exclusive-or b is " result-disp
*> More complex operations can be constructed from these.
COMPUTE result = B-NOT (a B-XOR b)
DISPLAY "Logical equivalence of a and b is " result-disp
COMPUTE result = (B-NOT a) B-AND b
DISPLAY "Logical implication of a and b is " result-disp
*> Shift and rotation operators were only added in COBOL 2023.
COMPUTE result = a B-SHIFT-L b
DISPLAY "a shifted left by b is " result-disp
COMPUTE result = b B-SHIFT-R a
DISPLAY "b shifted right by a is " result-disp
COMPUTE result = a B-SHIFT-LC b
DISPLAY "a rotated left by b is " result-disp
COMPUTE result = b B-SHIFT-RC a
DISPLAY "b rotated right by a is " result-disp
GOBACK.
END PROGRAM bitwise-ops.

View file

@ -0,0 +1,41 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. mf-bitwise-ops.
DATA DIVISION.
LOCAL-STORAGE SECTION.
01 result USAGE BINARY-LONG.
78 arg-len VALUE LENGTH OF result.
LINKAGE SECTION.
01 a USAGE BINARY-LONG.
01 b USAGE BINARY-LONG.
PROCEDURE DIVISION USING a, b.
main-line.
MOVE b TO result
CALL "CBL_AND" USING a, result, VALUE arg-len
DISPLAY "a and b is " result
MOVE b TO result
CALL "CBL_OR" USING a, result, VALUE arg-len
DISPLAY "a or b is " result
MOVE a TO result
CALL "CBL_NOT" USING result, VALUE arg-len
DISPLAY "Not a is " result
MOVE b TO result
CALL "CBL_XOR" USING a, result, VALUE arg-len
DISPLAY "a exclusive-or b is " result
MOVE b TO result
CALL "CBL_EQ" USING a, result, VALUE arg-len
DISPLAY "Logical equivalence of a and b is " result
MOVE b TO result
CALL "CBL_IMP" USING a, result, VALUE arg-len
DISPLAY "Logical implication of a and b is " result
GOBACK.
END PROGRAM mf-bitwise-ops.

View file

@ -0,0 +1,9 @@
def operations (x, y)
p! x, y,
x & y, x | y, x ^ y,
~x,
x << y, x >> y,
x.rotate_left(y), x.rotate_right(y)
end
operations 10, 2

View file

@ -0,0 +1,4 @@
$X -band $Y
$X -bor $Y
$X -bxor $Y
-bnot $X

View file

@ -0,0 +1,6 @@
$X -shl $Y
# Arithmetic right shift
$X -shr $Y
# Requires quite a stretch of the imagination to call this "native" support of right rotate, but it works
[System.Security.Cryptography.SHA256Managed].GetMethod('RotateRight', 'NonPublic, Static', $null, @([UInt32], [Int32]), $null).Invoke($null, @([uint32]$X, $Y))

View file

@ -0,0 +1,295 @@
# riscv assembly raspberry pico2 rp2350
# program bitwise.s
# connexion putty com3
/*********************************************/
/* CONSTANTES */
/********************************************/
/* for this file see risc-v task include a file */
.include "../../constantesRiscv.inc"
/*******************************************/
/* INITIALED DATAS */
/*******************************************/
.data
szMessStart: .asciz "Program riscv start.\r\n"
szCariageReturn: .asciz "\r\n"
szMessReg0: .asciz "Register s0:\r\n"
szMessReg1: .asciz "Register s1:\r\n"
szMessAnd: .asciz "And result:\r\n"
szMessOr: .asciz "Or result :\r\n"
szMessXor: .asciz "Xor result :\r\n"
szMessNot: .asciz "Not result :\r\n"
szMessNeg: .asciz "Neg result :\r\n"
szMessBset: .asciz "Set single bit result :\r\n"
szMessSll: .asciz "Shift left result :\r\n"
szMessSrl: .asciz "Shift right result :\r\n"
szMessSra: .asciz "Shift right arithmetic result :\r\n"
szMessRol: .asciz "Rotation left result :\r\n"
szMessRor: .asciz "Rotation right result :\r\n"
szMessBclr: .asciz "Clear single bit result :\r\n"
szMessBext: .asciz "Extraction single bit result :\r\n"
szMessBinv: .asciz "Inversion single bit result :\r\n"
szMessBrev: .asciz "Bit-reverse within each byte result :\r\n"
szMessClz: .asciz "Count leading zeroes result :\r\n"
szMessCtz: .asciz "Count trailing zeroes result :\r\n"
szMessCpop: .asciz "Population count result :\r\n"
szMessPack: .asciz "Pack two halfwords into one word result :\r\n"
szMessPackh: .asciz "Pack two byte into one halfword result :\r\n"
szMessZip: .asciz "Zip result :\r\n"
szMessUnzip: .asciz "Unzip result :\r\n"
.align 2
/*******************************************/
/* UNINITIALED DATA */
/*******************************************/
.bss
.align 2
sConvArea: .skip 24
sConvBinaire: .skip 36
/**********************************************/
/* SECTION CODE */
/**********************************************/
.text
.global main
main:
call stdio_init_all # général init
1: # start loop connexion
li a0,0 # raz argument register
call tud_cdc_n_connected # waiting for USB connection
beqz a0,1b # return code = zero ?
la a0,szMessStart # message address
call writeString # display message
la a0,szMessReg0 # message address
call writeString # display message
li s0,0b1100
mv a0,s0
call displayResult
la a0,szMessReg1 # message address
call writeString # display message
li s1,0b1001
mv a0,s1
call displayResult
la a0,szMessAnd # and
call writeString # display message
andi a0,s0,0b1001 # and immediat value
call displayResult
and a0,s0,s1 # register and
call displayResult
andn a0,s0,s1 # register and and not
call displayResult
la a0,szMessOr # or
call writeString # display message
ori a0,s0,0b1001 # or immediat value
call displayResult
or a0,s0,s1 # or register
call displayResult
orn a0,s0,s1 # or and not register
call displayResult
orc.b a0,s0 # OR-combine of bits within each byte. Generates a mask of nonzero bytes
call displayResult
la a0,szMessXor # xor
call writeString # display message
xori a0,s0,0b1001 # xor immediat value
call displayResult
xor a0,s0,s1 # xor register
call displayResult
xnor a0,s0,s1 # Bitwise XOR with inverted operand. Equivalently, bitwise NOT of bitwise XOR
call displayResult
la a0,szMessNot # not
call writeString # display message
not a0,s0 # not
call displayResult
la a0,szMessNeg # negation
call writeString # display message
neg a0,s0 # negation
call displayResult
la a0,szMessSll # shift left
call writeString # display message
slli a0,s0,11 # shift left immediat value
call displayResult
li t0,5
sll a0,s0,t0 # shift left register
call displayResult
la a0,szMessSrl # shift right
call writeString # display message
srli a0,s0,1 # shift right immediat value
call displayResult
li t0,5
srl a0,s0,t0 # shift right register
call displayResult
la a0,szMessSra # Shift right, arithmetic
call writeString # display message
srai a0,s0,1 # shift right arithmetic immediat value
call displayResult
li t0,5
li t1,0b10000000000000001100000000000000
sra a0,t1,t0 # shift right arithmetic register
call displayResult
la a0,szMessRol # rotation left
call writeString # display message
li t0,5
rol a0,s0,t0 # rotation left register
call displayResult
la a0,szMessRor # rotation right
call writeString # display message
rori a0,s0,10 # rotation right immediat value
call displayResult
li t0,5
ror a0,s0,t0 # rotation right register
call displayResult
la a0,szMessBclr # Clear single bit.
call writeString # display message
bclri a0,s0,3 # Clear single bit. immediat value
call displayResult
li t0,2
bclr a0,s0,t0 # Clear single bit. register
call displayResult
la a0,szMessBext # Extraction single bit.
call writeString # display message
bexti a0,s0,3 # Extraction single bit. immediat value
call displayResult
li t0,2
bext a0,s0,t0 # Extraction single bit. register
call displayResult
la a0,szMessBinv # Inversion single bit.
call writeString # display message
binvi a0,s0,3 # Inversion bit. immediat value
call displayResult
li t0,2
binv a0,s0,t0 # Inversion single bit. register
call displayResult
la a0,szMessBset # Set single bit
call writeString # display message
bseti a0,s0,8 # Set single bit (immediate).
call displayResult
li t0,17
bset a0,s0,t0 # Set single bit
call displayResult
la a0,szMessClz # Count leading zeroes (starting from MSB, searching LSB-ward).
call writeString # display message
clz a0,s0 #
call displayResultD
la a0,szMessCtz # Count trailing zeroes (starting from LSB, searching MSB-ward).
call writeString # display message
ctz a0,s0 #
call displayResultD
la a0,szMessCpop # Population count
call writeString # display message
cpop a0,s0 #
call displayResultD
la a0,szMessBrev # Bit-reverse within each byte
call writeString # display message
brev8 a0,s0 #
call displayResult
la a0,szMessPack # Pack two halfwords into one word
call writeString # display message
li t0,0x1234
li t1,0x5678
pack a0,t1,t0 #
call displayResultHex
la a0,szMessPackh # Pack two halfwords into one word
call writeString # display message
li t0,0x12
li t1,0x34
packh a0,t1,t0 #
call displayResultHex
la a0,szMessZip # Interleave upper/lower half of register into odd/even bits of result
call writeString # display message
zip s2,s0
mv a0,s2 #
call displayResult
la a0,szMessUnzip # Deinterleave odd/even bits of register into upper/lower half of result
call writeString # display message
unzip a0,s2 #
call displayResult
call getchar
100: # final loop
j 100b
/**********************************************/
/* display binary Result */
/**********************************************/
/* a0 value */
.equ LGZONECONV, 20
displayResult:
addi sp, sp, -4 # reserve stack
sw ra, 0(sp)
la a1,sConvBinaire # conversion result address
call conversion2 # binary conversion
la a0,sConvBinaire # message address
call writeString # display message
la a0,szCariageReturn
call writeString
100:
lw ra, 0(sp)
addi sp, sp, 4
ret
/**********************************************/
/* display Result décimal */
/**********************************************/
/* a0 value */
.equ LGZONECONV, 20
displayResultD:
addi sp, sp, -4 # reserve stack
sw ra, 0(sp)
la a1,sConvArea # conversion result address
call conversion10 # binary conversion
la a0,sConvArea # message address
call writeString # display message
la a0,szCariageReturn
call writeString
100:
lw ra, 0(sp)
addi sp, sp, 4
ret
/**********************************************/
/* display hexadecimal result */
/**********************************************/
/* a0 value */
.equ LGZONECONV, 20
displayResultHex:
addi sp, sp, -4 # reserve stack
sw ra, 0(sp)
la a1,sConvArea # conversion result address
call conversion16 # conversion decimal
la a0,sConvArea # message address
call writeString # display message
la a0,szCariageReturn
call writeString
100:
lw ra, 0(sp)
addi sp, sp, 4
ret
/************************************/
/* file include Fonctions */
/***********************************/
/* for this file see risc-v task include a file */
.include "../../includeFunctions.s"

View file

@ -0,0 +1,32 @@
Rebol [
title: "Rosetta code: Bitwise operations"
file: %Bitwise_operations.r3
url: https://rosettacode.org/wiki/Bitwise_operations
needs: 3.10.0
]
do-test: function [code][
res: try code
case [
error? res [res: join "error: " res/id]
integer? res [res: enbase to-binary res 2]
]
printf [-21 " == "] reduce [form code res]
]
foreach test [
[10]
[2]
[10 AND 2]
[10 & 2]
[10 OR 2]
[10 | 2]
[10 XOR 2]
[10 >> 2]
[10 << 2]
[-65432]
[complement -65432]
[255]
[shift 255 -2]
[shift 255 56]
[shift/logical 255 56]
][ do-test test ]

View file

@ -0,0 +1,9 @@
a ← 10
b ← 2
# Experimental!
¬a # bitwise NOT
°⋯ /× ⋯ ⊟ a b # bitwise AND
°⋯ / ⋯ ⊟ a b # bitwise OR
°⋯ /≠ ⋯ ⊟ a b # bitwise XOR
* a ⁿ:2 b # SHL
⌊÷: a ⁿ:2 b # ASR

View file

@ -0,0 +1,9 @@
fn bitwise(a int, b int) {
println("a AND b: ${a & b}")
println("a OR b: ${a | b}")
println("a XOR b: ${a ^ b}")
println("NOT a: ${~a}")
println("a SHL b: ${a << b}") // left shift
println("a SHR b: ${a >> b}") // right shift
println("a USHR b: ${a >>> b}") // unsigned right shift
}