Open In App

Bitwise Logical Operations in R

Improve
Improve
Like Article
Like
Save
Share
Report

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:

OPERATOR DESCRIPTION SYNTAX
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




# bitwise or for 2 and 3
print(bitwOr(2,3))
 
# bitwise or for 2 and 4
print(bitwOr(2,4))
 
# bitwise or for 2 and 5
print(bitwOr(2,5))
 
# bitwise or for 78 and 0
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)
 
# s and a are the two vectors
a=c(90,91,92,93,94)
 
# bitwise or operation between
# a and s vector elements
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




# range of a values
# each value of a with
# respect to b
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
 
# bitwise on 3
print(bitwNot(a))
 
# bitwise on 6
print(bitwNot(b))
print("----------------")
 
# consider values from 1 to 15
# to perform this operation
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




# create two vectors a and
# b  with elements
a=c(7058,7059,7056)
b=c(34,45,63)
 
# bitwise on vector a
print(bitwNot(a))
 
# bitwise on vector b
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




# bitwise operator between 4 and 3
print(bitwAnd(4,3))
 
# bitwise operator between 4 and 7
print(bitwAnd(4,7))
 
# bitwise operator between  1and 4
print(bitwAnd(1,4))
 
# bitwise operator between 56 and 8
print(bitwAnd(56,8))
 
# bitwise operator between 4 and 0
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)
 
# s and a are the two vectors
a=c(90,91,92,93,94)
 
# bitwise And operation between
# a and s vector elements
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




# shift 1 by 4 bit
bitwShiftL(1,4)
 
# shift 2 by 4 bit
bitwShiftL(2,4)
 
# shift 3 by 4 bit
bitwShiftL(3,4)
 
# shift 4 by 4 bit
bitwShiftL(4,4)
 
# shift 5 by 4 bit
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




# shift 4 by 1 bit
bitwShiftR(1,4)
 
# shift 4 by 2 bit
bitwShiftR(4,2)
 
# shift 3 by 4 bit
bitwShiftR(3,4)
 
# shift 16 by 2 bit
bitwShiftR(16,2)
 
# shift 8 by 2 bit
bitwShiftR(8,2)


Output:

0
1
0
4
2


Last Updated : 07 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads