Logical operations

Revision as of 21:36, 18 November 2007 by Ce (talk | contribs) (new task, C, C++, FORTRAN)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Write a function that takes two logical (boolean) values, and outputs the result of "and" and "or" on both arguments as well as "not" on the first arguments. If the programming language doesn't provide a separate type for logical values, use the type most commonly used for that purpose.

Task
Logical operations
You are encouraged to solve this task according to the task description, using any language you may know.

Basic Data Operation
This is a basic data operation. It represents a fundamental action on a basic data type.

You may see other such operations in the Basic Data Operations category, or:

Integer Operations
Arithmetic | Comparison

Boolean Operations
Bitwise | Logical

String Operations
Concatenation | Interpolation | Comparison | Matching

Memory Operations
Pointers & references | Addresses

C

void print_logic(int a, int b)
{
  printf("a and b is %d\n", a && b);
  printf("a or b is %d\n", a || b);
  printf("not a is %d\n", !a);
}

C plus plus

void print_logic(bool a, bool b)
{
  std::cout << std::boolalpha; // so that bools are written as "true" and "false"
  std::cout << "a and b is " << (a && b) << "\n";
  std::cout << "a or b is " << (a || b) << "\n";
  std::cout << "not a is " << (!a) << "\n";
}

Fortran

      SUBROUTINE PRNLOG(A, B)
      LOGICAL A, B
      PRINT *, 'a and b is ', A .AND. B
      PRINT *, 'a or b is ', A .OR. B
      PRINT *, 'not a is ', .NOT. A
      END