B
This commit is contained in:
parent
e5e8880e41
commit
518da4a923
1019 changed files with 15877 additions and 0 deletions
5
Task/Bitwise-operations/R/bitwise-operations-1.r
Normal file
5
Task/Bitwise-operations/R/bitwise-operations-1.r
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
a <- as.hexmode(35)
|
||||
b <- as.hexmode(42)
|
||||
as.integer(a & b) # 34
|
||||
as.integer(a | b) # 43
|
||||
as.integer(xor(a, b)) # 9
|
||||
13
Task/Bitwise-operations/R/bitwise-operations-2.r
Normal file
13
Task/Bitwise-operations/R/bitwise-operations-2.r
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
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
|
||||
8
Task/Bitwise-operations/R/bitwise-operations-3.r
Normal file
8
Task/Bitwise-operations/R/bitwise-operations-3.r
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
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-4.r
Normal file
14
Task/Bitwise-operations/R/bitwise-operations-4.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))
|
||||
Loading…
Add table
Add a link
Reference in a new issue