Bitwise Logical Operations in R
Bitwise logical operations are used to perform logical operations bit by bit in a number. This article discusses how bitwise logical operators are used in the R programming language.
The logical operations include:
bitwOr |
bitwise OR |
bitwOr(value1,value2) |
bitwXor |
bitwise XOR |
bitwXor(value1,value2) |
bitwNot |
bitwise NOT |
bitwNot(value) |
bitwAnd |
bitwise AND |
bitwAnd(value1,value2) |
bitwShiftL |
Left shift |
bitwShiftL(value,shift) |
bitwShiftR |
Right shift |
bitwShiftR(value,shift) |
Let’s discuss these operators in detail.
bitwOr
R programming language uses bitwOr function to perform Bitwise OR operation on two numbers. It will take two values as input and return the value. Where value can be the numbers or lists/vectors.
Example:
2=0010
3=0011
bitwOr(2,3) ---> 0010
& 0011
--------------
0011 ---> 3
Program:
R
print ( bitwOr (2,3))
print ( bitwOr (2,4))
print ( bitwOr (2,5))
print ( bitwOr (78,0))
|
Output:
[1] 3
[1] 6
[1] 7
[1] 78
bitwOr can also be performed on vectors
Program:
R
s= c (1,2,3,4,5)
a= c (90,91,92,93,94)
print ( bitwOr (a,s))
|
Output:
[1] 91 91 95 93 95
bitwXor
In R, we can perform the bitwise xor operation using the function called bitwXor() function. This function takes two data at a time as input and perform the following operation. It converts the numbers into binary values (bits – 0’s and 1’s). Then it returns 0 if both are same, otherwise 1.
Input values can be integers or list/vector.
Example:
5 - 0101
6 - 0110
bitwXOR(5,6) ---> 0101
^ 0110
---------------
0011 ---> 3
Program
R
a=10:20
b=58
for (i in a){
print ( bitwXor (i,b))
}
|
Output:
[1] 48
[1] 49
[1] 54
[1] 55
[1] 52
[1] 53
[1] 42
[1] 43
[1] 40
[1] 41
[1] 46
bitwNot
Bitwise not operator is used to return the inverse value of the given number i.e. if 0 then 1, if 1 then 0. In R, we can find bitwise not of the given number using function called bitwNot(). The input for this function can also be an integer, vector, matrix , list etc..
Example:
6- 0110
bitwNot(6) ----> 0110
+ 1
-----------
0111 ---> -7
Program
R
a=3
b=6
print ( bitwNot (a))
print ( bitwNot (b))
print ( "----------------" )
values=1:15
for (i in values){
print ( bitwNot (i))
}
|
Output:
[1] -4
[1] -7
[1] "----------------"
[1] -2
[1] -3
[1] -4
[1] -5
[1] -6
[1] -7
[1] -8
[1] -9
[1] -10
[1] -11
[1] -12
[1] -13
[1] -14
[1] -15
[1] -16
This can also be applied to vectors.
Program
Python
a = c( 7058 , 7059 , 7056 )
b = c( 34 , 45 , 63 )
print (bitwNot(a))
print (bitwNot(b))
|
Output:
[1] -7059 -7060 -7057
[1] -35 -46 -64
bitwAnd
R language uses bitwAnd function to perform bitwise and operation. Where input values can be integers or list/vector.
Example:
3 - 0011
4 - 0100
bitwAnd(3,4) ----> 0011
& 0100
--------------
0000 ---> 0
Program
R
print ( bitwAnd (4,3))
print ( bitwAnd (4,7))
print ( bitwAnd (1,4))
print ( bitwAnd (56,8))
print ( bitwAnd (4,0))
|
Output:
[1] 0
[1] 4
[1] 0
[1] 8
[1] 0
bitwise AND can also be performed on vectors.
Program
R
s= c (1,2,3,4,5)
a= c (90,91,92,93,94)
print ( bitwAnd (s,a))
|
Output:
[1] 0 2 0 4 4
bitwShiftL
R language uses bitwShiftL function to perform bitwise left shift operation. The input to the function is shift value and an integer/vector/list
Formula:
N*(2^i)
Where N is the given number and i is the number of shifts.
Example:
bitwShiftL(1,4) - N=1 and i=4
so, N*(2^i)
1*(2^4)=16
Program
R
bitwShiftL (1,4)
bitwShiftL (2,4)
bitwShiftL (3,4)
bitwShiftL (4,4)
bitwShiftL (5,4)
|
Output:
16
32
48
64
80
bitwShiftR
It will perform the right shift operation. It will shift the bits from left to right.
Formula:
N/(2^i)
where N is the given number and i is the no of right shifts.
Example:
bitwShiftR(4,1)
here N=4 and i=1
so 4/(2^1)=2
Program
R
bitwShiftR (1,4)
bitwShiftR (4,2)
bitwShiftR (3,4)
bitwShiftR (16,2)
bitwShiftR (8,2)
|
Output:
0
1
0
4
2
Last Updated :
07 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...