Sunday, September 1, 2024

BCA PART A 7.R program to implement logic gates (not, and, or, xor).

 

Logic gates are fundamental building blocks of digital circuits. They perform basic logical functions that are essential for digital computation. Each gate takes one or more binary inputs (0s and 1s) and produces a single binary output based on the type of gate.

Types of Logic Gates:

  1. NOT Gate (Inverter):

    • Function: Inverts the input.
    • Input/Output:
      • If input is 1, output is 0.
      • If input is 0, output is 1.
  2. AND Gate:

    • Function: Outputs 1 only if all inputs are 1.
    • Input/Output:
      • If both inputs are 1, output is 1.
      • If either or both inputs are 0, output is 0.
  3. OR Gate:

    • Function: Outputs 1 if at least one input is 1.
    • Input/Output:
      • If any input is 1, output is 1.
      • If all inputs are 0, output is 0.
  4. XOR Gate (Exclusive OR):

    • Function: Outputs 1 if the inputs are different.
    • Input/Output:
      • If inputs are 1, 0 or 0, 1, output is 1.
      • If inputs are 0, 0 or 1, 1, output is 0.

Why Are Logic Gates Important?

Logic gates are crucial for performing calculations and making decisions in digital devices. They are used in everything from simple circuits in household electronics to complex processors in computers. By combining these gates, more complex operations like addition, multiplication, and data storage are made possible.


Program

# Function for NOT gate

NOT <- function(x) {

  if (x == 1) {

    return(0)

  } else if (x == 0) {

    return(1)

  } else {

    stop("Invalid input. Input must be 0 or 1.")

  }

}


# Function for AND gate

AND <- function(x, y) {

  if (x == 1 && y == 1) {

    return(1)

  } else if ((x == 0 || x == 1) && (y == 0 || y == 1)) {

    return(0)

  } else {

    stop("Invalid input. Inputs must be 0 or 1.")

  }

}


# Function for OR gate

OR <- function(x, y) {

  if (x == 1 || y == 1) {

    return(1)

  } else if (x == 0 && y == 0) {

    return(0)

  } else {

    stop("Invalid input. Inputs must be 0 or 1.")

  }

}


# Function for XOR gate

XOR <- function(x, y) {

  if ((x == 1 && y == 0) || (x == 0 && y == 1)) {

    return(1)

  } else if ((x == 0 && y == 0) || (x == 1 && y == 1)) {

    return(0)

  } else {

    stop("Invalid input. Inputs must be 0 or 1.")

  }

}


# Example usage of logic gates

x <- 1

y <- 0


cat("NOT(", x, ") =", NOT(x), "\n")

cat("AND(", x, ",", y, ") =", AND(x, y), "\n")

cat("OR(", x, ",", y, ") =", OR(x, y), "\n")

cat("XOR(", x, ",", y, ") =", XOR(x, y), "\n")


Output

Rscript /tmp/uXYsr5vwum.r
NOT( 1 ) = 0 
AND( 1 , 0 ) = 0 
OR( 1 , 0 ) = 1 
XOR( 1 , 0 ) = 1 

Explanation

1. NOT Gate (NOT function):

  • Purpose: The NOT gate inverts the input. If the input is 1, it outputs 0, and if the input is 0, it outputs 1.
  • How it works: The function takes a single input x. If x is 1, it returns 0. If x is 0, it returns 1.

2. AND Gate (AND function):

  • Purpose: The AND gate outputs 1 only if both inputs are 1. For any other combination (i.e., 0, 0, 1, 0, or 0, 1), it outputs 0.
  • How it works: The function takes two inputs x and y. It checks if both x and y are 1. If they are, it returns 1. Otherwise, it returns 0.

3. OR Gate (OR function):

  • Purpose: The OR gate outputs 1 if at least one of the inputs is 1. It only outputs 0 if both inputs are 0.
  • How it works: The function takes two inputs x and y. It checks if either x or y is 1. If either is 1, it returns 1. If both are 0, it returns 0.

4. XOR Gate (XOR function):

  • Purpose: The XOR gate (exclusive OR) outputs 1 if the inputs are different (i.e., 0, 1 or 1, 0). If the inputs are the same (i.e., 0, 0 or 1, 1), it outputs 0.
  • How it works: The function takes two inputs x and y. It checks if the inputs are different. If they are, it returns 1. If they are the same, it returns 0.

No comments:

Post a Comment