Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please write your code in R and submit your script and results for each of the f

ID: 3724348 • Letter: P

Question

Please write your code in R and submit your script and results for each of the following.

6.Create two character vectors S11 and S12 such that S11 contains the binary representations of the numbers (5,2,1,6,3,4,7,0), and S12 contains those of the numbers (1,4,6,2,0,7,5,3). (10 points)

7.Write a function called S11_function that takes a block of 4-character binary (such as “1110”) and, using the two s-boxes from question 6, returns a 3-character binary number using the logic that if the first character is “0” then use S11, otherwise use S12. (20 points)

Explanation / Answer

#Function to covert decimal number into binary

binary<-function(p_number) {
bsum<-0
bexp<-1
while (p_number > 0) {
     digit<-p_number %% 2
     p_number<-floor(p_number / 2)
     bsum<-bsum + digit * bexp
     bexp<-bexp * 10
}
return(bsum)
}

#This function takes Decimal vector as input and returns a vector that is Binary form of respective elements

convert_to_binary <- function(vector) {
for (i in 1:length(vector)) {
        if(vector[i] > 1) {
            vector[i]=binary(vector[i])#here we are converting individual elements from vector into binary by calling binary() function
    }
}
vector
}

# convering and storing mentioned decimal vectors into binary form using convert_to_binary() function
S11 <<- convert_to_binary(c(5,2,1,6,3,4,7,0))
S11 <<- convert_to_binary(c(1,4,6,2,0,7,5,3))

# I am providing solution only for 6th one as i'm not sure what you are asking from 7th one, I will be happy to help if you elaborate it further