Bitwise Logical Operations in R - GeeksforGeeks

08 May, 2021
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:
Let’s discuss these operators in detail.
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
# bitwise or for 2 and 3print(bitwOr(2,3))  # bitwise or for 2 and 4print(bitwOr(2,4))  # bitwise or for 2 and 5print(bitwOr(2,5))  # bitwise or for 78 and 0print(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)  # s and a are the two vectorsa=c(90,91,92,93,94)  # bitwise or operation between# a ans s vector elementsprint(bitwOr(a,s))
Output:
[1] 91 91 95 93 95
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
# range of a values# each value of a with # respect to ba=10:20b=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
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=3b=6  # bitwise on 3print(bitwNot(a))  # bitwise on 6print(bitwNot(b))print("----------------")  # consider values from 1 to 15 # to perform this operationvalues=1:15for (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
# create two vectors a and# b  with elementsa=c(7058,7059,7056)b=c(34,45,63)  # bitwise on vector aprint(bitwNot(a))  # bitwise on vector bprint(bitwNot(b))
Output:
[1] -7059 -7060 -7057
[1] -35 -46 -64
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
# bitwise operator between 4 and 3print(bitwAnd(4,3))  # bitwise operator between 4 and 7print(bitwAnd(4,7))  # bitwise operator between  1and 4print(bitwAnd(1,4))  # bitwise operator between 56 and 8print(bitwAnd(56,8))  # bitwise operator between 4 and 0print(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)  # s and a are the two vectorsa=c(90,91,92,93,94)  # bitwise And operation between# a ans s vector elementsprint(bitwAnd(s,a))
Output:
[1] 0 2 0 4 4
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
# shift 4 by 1 bitbitwShiftL(1,4)  # shift 4 by 2 bitbitwShiftL(2,4)  # shift 4 by 3 bitbitwShiftL(3,4)  # shift 4 by 4 bitbitwShiftL(4,4)  # shift 4 by 5 bitbitwShiftL(5,4)
Output:
16
32
48
64
80
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:
bitShiftR(4,1)
here N=4 and i=1
so 4*(2/1)=8
Program
R
# shift 1 by 4 bitbitwShiftR(1,4)  # shift 4 by 2 bitbitwShiftR(4,2)  # shift 4 by 3 bitbitwShiftR(3,4)  # shift 16 by 2 bitbitwShiftR(16,2)  # shift 8 by 2 bitbitwShiftR(8,2)
Output:
0
1
0
4
2
R-basics
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
How to import an Excel File into R ?
How to filter R dataframe by multiple conditions?
Replace Specific Characters in String in R
Time Series Analysis in R
R - if statement