Logical operations: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|Forth}}: convert to well-formed flags)
(→‎{{header|Ada}}: - Added Ada example)
Line 3: Line 3:


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.
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.

=={{header|Ada}}==

I have also included logical xor because it is defined for Ada boolean types.
All the operators below work equally well on arrays of boolean types.

procedure Print_Logic(A : Boolean; B : Boolean) is
begin
Put_Line("A and B is " & Boolean'Image(A and B));
Put_Line("A or B is " & Boolean'Image(A or B));
Put_Line("A xor B is " & Boolean'Image(A xor B));
Put_Line("not A is " & Boolean'Image(not A));
end Print_Logic;


=={{header|C}}==
=={{header|C}}==

Revision as of 02:30, 19 November 2007

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


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.

Ada

I have also included logical xor because it is defined for Ada boolean types. All the operators below work equally well on arrays of boolean types.

procedure Print_Logic(A : Boolean; B : Boolean) is
begin
   Put_Line("A and B is " & Boolean'Image(A and B));
   Put_Line("A or B  is " & Boolean'Image(A or B));
   Put_Line("A xor B is " & Boolean'Image(A xor B));
   Put_Line("not A   is " & Boolean'Image(not A));
end Print_Logic;

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";
}

Forth

Forth can use bitwise operators if the boolean values are well formed: TRUE (-1) and FALSE (0). 0<> converts an ill-formed flag (zero/non-zero) to a well-formed flag (false/true).

: .bool ( ? -- ) if ." true" else ." false" then ;
: logic ( a b -- ) 0<> swap 0<> swap
 cr ." a = " over .bool ."   b = " dup .bool
 cr ." a and b = " 2dup and .bool
 cr ." a  or b = " over  or .bool
 cr ." not a = " 0= .bool ;

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

Java

public static void logic(boolean a, boolean b){
  System.out.println("a AND b: " + (a && b));
  System.out.println("a OR b: " + (a || b));
  System.out.println("NOT a: " + (!a));
}

Additionally, ^ is used for XOR and == is used for "equal to" (a.k.a. bidirectional implication).

MAXScript

fn printLogic a b =
(
    format "a and b is %\n" (a and b)
    format "a or b is %\n" (a or b)
    format "not a is %\n" (not a)
)

Perl

sub print_logic
{
	my ($a, $b)=@_;
	print "a and b is ${\($a && $b)}\n";
	print "a or b is ${\($a || $b)}\n";
	print "not a is ${\( ! $a)}\n";
}