Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
35
Task/Bitwise-operations/COBOL/bitwise-operations-1.cobol
Normal file
35
Task/Bitwise-operations/COBOL/bitwise-operations-1.cobol
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
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) COMP.
|
||||
|
||||
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
|
||||
|
||||
*> COBOL does not have shift or rotation operators.
|
||||
|
||||
GOBACK
|
||||
.
|
||||
41
Task/Bitwise-operations/COBOL/bitwise-operations-2.cobol
Normal file
41
Task/Bitwise-operations/COBOL/bitwise-operations-2.cobol
Normal 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
|
||||
.
|
||||
25
Task/Bitwise-operations/Fortran/bitwise-operations-3.f
Normal file
25
Task/Bitwise-operations/Fortran/bitwise-operations-3.f
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
program bits_rosetta
|
||||
implicit none
|
||||
|
||||
call bitwise(14,3)
|
||||
|
||||
contains
|
||||
|
||||
subroutine bitwise(a,b)
|
||||
implicit none
|
||||
integer, intent(in):: a,b
|
||||
character(len=*), parameter :: fmt1 = '(2(a,i10))'
|
||||
character(len=*),parameter :: fmt2 = '(3(a,b32.32),i20)'
|
||||
|
||||
write(*,fmt1) 'input a=',a,' b=',b
|
||||
write(*,fmt2) 'and : ', a,' & ',b,' = ',iand(a, b),iand(a, b)
|
||||
write(*,fmt2) 'or : ', a,' | ',b,' = ',ior(a, b),ior(a, b)
|
||||
write(*,fmt2) 'xor : ', a,' ^ ',b,' = ',ieor(a, b),ieor(a, b)
|
||||
write(*,fmt2) 'lsh : ', a,' << ',b,' = ',ishft(a, abs(b)),ishft(a, abs(b))
|
||||
write(*,fmt2) 'rsh : ', a,' >> ',b,' = ',ishft(a, -abs(b)),ishft(a, -abs(b))
|
||||
write(*,fmt2) 'not : ', a,' ~ ',b,' = ',not(a),not(a)
|
||||
write(*,fmt2) 'rot : ', a,' r ',b,' = ',ishftc(a,-abs(b)),ishftc(a,-abs(b))
|
||||
|
||||
end subroutine bitwise
|
||||
|
||||
end program bits_rosetta
|
||||
8
Task/Bitwise-operations/Fortran/bitwise-operations-4.f
Normal file
8
Task/Bitwise-operations/Fortran/bitwise-operations-4.f
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
Input a= 14 b= 3
|
||||
AND : 00000000000000000000000000001110 & 00000000000000000000000000000011 = 00000000000000000000000000000010 2
|
||||
OR : 00000000000000000000000000001110 | 00000000000000000000000000000011 = 00000000000000000000000000001111 15
|
||||
XOR : 00000000000000000000000000001110 ^ 00000000000000000000000000000011 = 00000000000000000000000000001101 13
|
||||
LSH : 00000000000000000000000000001110 << 00000000000000000000000000000011 = 00000000000000000000000001110000 112
|
||||
RSH : 00000000000000000000000000001110 >> 00000000000000000000000000000011 = 00000000000000000000000000000001 1
|
||||
NOT : 00000000000000000000000000001110 ~ 00000000000000000000000000000011 = 11111111111111111111111111110001 -15
|
||||
ROT : 00000000000000000000000000001110 ~ 00000000000000000000000000000011 = 11000000000000000000000000000001 -1073741823
|
||||
|
|
@ -1,5 +1,12 @@
|
|||
a <- as.hexmode(35)
|
||||
b <- as.hexmode(42)
|
||||
as.integer(a & b) # 34
|
||||
as.integer(a | b) # 43
|
||||
as.integer(xor(a, b)) # 9
|
||||
# Since R 3.0.0, the base package provides bitwise operators, see ?bitwAnd
|
||||
|
||||
a <- 35
|
||||
b <- 42
|
||||
bitwAnd(a, b)
|
||||
bitwOr(a, b)
|
||||
bitwXor(a, b)
|
||||
bitwNot(a)
|
||||
bitwShiftL(a, 2)
|
||||
bitwShiftR(a, 2)
|
||||
|
||||
# See also http://cran.r-project.org/src/base/NEWS.html
|
||||
|
|
|
|||
|
|
@ -1,13 +1,5 @@
|
|||
intToLogicalBits <- function(intx) as.logical(intToBits(intx))
|
||||
logicalBitsToInt <- function(lb) as.integer(sum((2^(0:31))[lb]))
|
||||
"%AND%" <- function(x, y)
|
||||
{
|
||||
logicalBitsToInt(intToLogicalBits(x) & intToLogicalBits(y))
|
||||
}
|
||||
"%OR%" <- function(x, y)
|
||||
{
|
||||
logicalBitsToInt(intToLogicalBits(x) | intToLogicalBits(y))
|
||||
}
|
||||
|
||||
35 %AND% 42 # 34
|
||||
35 %OR% 42 # 42
|
||||
a <- as.hexmode(35)
|
||||
b <- as.hexmode(42)
|
||||
as.integer(a & b) # 34
|
||||
as.integer(a | b) # 43
|
||||
as.integer(xor(a, b)) # 9
|
||||
|
|
|
|||
|
|
@ -1,8 +1,13 @@
|
|||
library(bitops)
|
||||
bitAnd(35, 42) # 34
|
||||
bitOr(35, 42) # 43
|
||||
bitXor(35, 42) # 9
|
||||
bitFlip(35, bitWidth=8) # 220
|
||||
bitShiftL(35, 1) # 70
|
||||
bitShiftR(35, 1) # 17
|
||||
# Note that no bit rotation is provided in this package
|
||||
intToLogicalBits <- function(intx) as.logical(intToBits(intx))
|
||||
logicalBitsToInt <- function(lb) as.integer(sum((2^(0:31))[lb]))
|
||||
"%AND%" <- function(x, y)
|
||||
{
|
||||
logicalBitsToInt(intToLogicalBits(x) & intToLogicalBits(y))
|
||||
}
|
||||
"%OR%" <- function(x, y)
|
||||
{
|
||||
logicalBitsToInt(intToLogicalBits(x) | intToLogicalBits(y))
|
||||
}
|
||||
|
||||
35 %AND% 42 # 34
|
||||
35 %OR% 42 # 42
|
||||
|
|
|
|||
|
|
@ -1,14 +1,8 @@
|
|||
# As one can see from
|
||||
getDLLRegisteredRoutines(getLoadedDLLs()$base)
|
||||
# R knows functions bitwiseAnd, bitwiseOr, bitwiseXor and bitwiseNot.
|
||||
# Here is how to call them (see ?.Call for the calling mechanism):
|
||||
|
||||
.Call("bitwiseOr", as.integer(12), as.integer(10))
|
||||
.Call("bitwiseXor", as.integer(12), as.integer(10))
|
||||
.Call("bitwiseAnd", as.integer(12), as.integer(10))
|
||||
.Call("bitwiseNot", as.integer(12))
|
||||
|
||||
# It would be easy to embed these calls in R functions, for better readability
|
||||
# Also, it's possible to call these functions on integer vectors:
|
||||
|
||||
.Call("bitwiseOr", c(5L, 2L), c(3L, 8L))
|
||||
library(bitops)
|
||||
bitAnd(35, 42) # 34
|
||||
bitOr(35, 42) # 43
|
||||
bitXor(35, 42) # 9
|
||||
bitFlip(35, bitWidth=8) # 220
|
||||
bitShiftL(35, 1) # 70
|
||||
bitShiftR(35, 1) # 17
|
||||
# Note that no bit rotation is provided in this package
|
||||
|
|
|
|||
14
Task/Bitwise-operations/R/bitwise-operations-5.r
Normal file
14
Task/Bitwise-operations/R/bitwise-operations-5.r
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
# As one can see from
|
||||
getDLLRegisteredRoutines(getLoadedDLLs()$base)
|
||||
# R knows functions bitwiseAnd, bitwiseOr, bitwiseXor and bitwiseNot.
|
||||
# Here is how to call them (see ?.Call for the calling mechanism):
|
||||
|
||||
.Call("bitwiseOr", as.integer(12), as.integer(10))
|
||||
.Call("bitwiseXor", as.integer(12), as.integer(10))
|
||||
.Call("bitwiseAnd", as.integer(12), as.integer(10))
|
||||
.Call("bitwiseNot", as.integer(12))
|
||||
|
||||
# It would be easy to embed these calls in R functions, for better readability
|
||||
# Also, it's possible to call these functions on integer vectors:
|
||||
|
||||
.Call("bitwiseOr", c(5L, 2L), c(3L, 8L))
|
||||
|
|
@ -1,8 +1,13 @@
|
|||
def bitwise(a, b)
|
||||
puts "a and b: #{a & b}"
|
||||
puts "a or b: #{a | b}"
|
||||
puts "a xor b: #{a ^ b}"
|
||||
puts "not a: #{~a}"
|
||||
puts "a << b: #{a << b}" # left shift
|
||||
puts "a >> b: #{a >> b}" # arithmetic right shift
|
||||
form = "%1$7s:%2$6d %2$016b"
|
||||
puts form % ["a", a]
|
||||
puts form % ["b", b]
|
||||
puts form % ["a and b", a & b]
|
||||
puts form % ["a or b ", a | b]
|
||||
puts form % ["a xor b", a ^ b]
|
||||
puts form % ["not a ", ~a]
|
||||
puts form % ["a << b ", a << b] # left shift
|
||||
puts form % ["a >> b ", a >> b] # arithmetic right shift
|
||||
end
|
||||
|
||||
bitwise(14,3)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue