Open In App

Convert an Integer to a Binary value in R Programming – as.binary() Function

as.binary() function in R Language is used to convert an integer value to a binary value.

Syntax: as.binary(x)



Parameters:
x: Integer value

Example 1:






# R Program to convert
# an integer to binary
  
# Loading library
library(binaryLogic)
  
# Calling as.binary() function
as.binary(1)
as.binary(10)
as.binary(-1)
as.binary(0xAB)

Output:

[1] 1
[1] 1 0 1 0
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[1] 1 0 1 0 1 0 1 1

Example 2:




# R Program to convert
# an integer to binary
  
# Loading library
library(binaryLogic)
  
# Creating a vector
x1 <- c(1, 10, -1, 2, 5)
  
# Calling as.binary() function
as.binary(x1)

Output:

[[1]]
[1] 1

[[2]]
[1] 1 0 1 0

[[3]]
 [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

[[4]]
[1] 1 0

[[5]]
[1] 1 0 1
Article Tags :