Difference between revisions of "And"

From Lazarus wiki
Jump to navigationJump to search
(→‎Bitwise operation: remove non-typical usage example (now is power of two))
(review)
Line 1: Line 1:
 
{{And}}
 
{{And}}
 +
 +
The binary operator <syntaxhighlight lang="pascal" enclose="none">and</syntaxhighlight> performs a logical conjunction.
 +
[[FPC]] also does a bitwise <syntaxhighlight lang="pascal" enclose="none">and</syntaxhighlight> when supplied with ordinal types.
  
 
== Boolean operation ==
 
== Boolean operation ==
 +
The operator <syntaxhighlight lang="pascal" enclose="none">and</syntaxhighlight> accepts to two Boolean type values.
 +
It is the logical conjunction written in classic logic as <math>A \land B</math>.
 +
Electrical engineers may write <math>A \times B</math> or <math>A \cdot B</math>, or eliminating the multiplication sign altogether writing <math>AB</math>.
 +
However, the [[*|asterisk]] has a different meaning in programming.
 +
The Boolean <syntaxhighlight lang="pascal" enclose="none">and</syntaxhighlight> evaluates to [[false and true|<syntaxhighlight lang="pascal" enclose="none">true</syntaxhighlight>]] if and only if both operands are <syntaxhighlight lang="pascal" enclose="none">true</syntaxhighlight>.
  
Boolean '''And''' produces a value of [[True|true]] if and only if both of its Boolean operands are true.
+
{| class="wikitable" style="text-align:center; margin:auto;"
 
+
! <syntaxhighlight lang="pascal" enclose="none">A</syntaxhighlight>
=== Truth table ===
+
! <syntaxhighlight lang="pascal" enclose="none">B</syntaxhighlight>
 
+
! <syntaxhighlight lang="pascal" enclose="none">A or B</syntaxhighlight>
{| class="wikitable"
 
 
|-
 
|-
! A !! B !! A and B
+
| <syntaxhighlight lang="pascal" enclose="none">false</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" enclose="none">false</syntaxhighlight>
 +
| style="background: #eeeeee" | <syntaxhighlight lang="pascal" enclose="none">false</syntaxhighlight>
 
|-
 
|-
| &nbsp; false &nbsp;|| &nbsp; false &nbsp;
+
| <syntaxhighlight lang="pascal" enclose="none">false</syntaxhighlight>
|style="background: #eeeeee" | &nbsp; false
+
| <syntaxhighlight lang="pascal" enclose="none">true</syntaxhighlight>
 +
| style="background: #eeeeee" | <syntaxhighlight lang="pascal" enclose="none">false</syntaxhighlight>
 
|-
 
|-
| &nbsp; false || &nbsp; true
+
| <syntaxhighlight lang="pascal" enclose="none">true</syntaxhighlight>
|style="background: #eeeeee" | &nbsp; false
+
| <syntaxhighlight lang="pascal" enclose="none">false</syntaxhighlight>
 +
| style="background: #eeeeee" | <syntaxhighlight lang="pascal" enclose="none">false</syntaxhighlight>
 
|-
 
|-
| &nbsp; true || &nbsp; false
+
| <syntaxhighlight lang="pascal" enclose="none">true</syntaxhighlight>
|style="background: #eeeeee" | &nbsp; false
+
| <syntaxhighlight lang="pascal" enclose="none">true</syntaxhighlight>
|-
+
| style="background: #eeeeee" | <syntaxhighlight lang="pascal" enclose="none">true</syntaxhighlight>
| &nbsp; true || &nbsp; true  
+
|+ truth table for logical conjunction
|style="background: #eeeeee" | &nbsp; true
 
 
|}
 
|}
  
 
== Bitwise operation ==
 
== Bitwise operation ==
 +
FPC also defines a bitwise <syntaxhighlight lang="pascal" enclose="none">and</syntaxhighlight>.
 +
Taking two ordinal operands logical <syntaxhighlight lang="pascal" enclose="none">and</syntaxhighlight> is calculated bit by bit:
 +
    1010'1100
 +
and 0011'0100
 +
――――――――――――
 +
    0010'0100
  
Logical '''And''' (aka Bitwise And) requires ordinal operands and sets a bit in the result variable to 1 if and only if both of the corresponding bits in the operands are 1.
+
== comparative remarks ==
 
+
Depending on the compiler's specific implementation of the data type [[Set|<syntaxhighlight lang="pascal" enclose="none">set</syntaxhighlight>]], the [[symmetric difference|symmetric difference of sets <syntaxhighlight lang="pascal" enclose="none">><</syntaxhighlight>]] virtually does the same as the bitwise <syntaxhighlight lang="pascal" enclose="none">and</syntaxhighlight>.
== See also ==
 
* [[Not]]
 
* [[Or]]
 
* [[Shl]]
 
 
 
* [[Const]]
 
* [[Function]]
 
* [[Integer]]
 
 
 
* [[Shl# Clear a bit|Clear a bit]]
 
* [[Bit manipulation]]
 
  
[[Category:Pascal]]
+
== see also ==
 +
* [[Or|<syntaxhighlight lang="pascal" enclose="none">or</syntaxhighlight>]]

Revision as of 13:13, 6 November 2018

Deutsch (de) English (en) español (es) suomi (fi) français (fr) русский (ru)

The binary operator and performs a logical conjunction. FPC also does a bitwise and when supplied with ordinal types.

Boolean operation

The operator and accepts to two Boolean type values. It is the logical conjunction written in classic logic as [math]\displaystyle{ A \land B }[/math]. Electrical engineers may write [math]\displaystyle{ A \times B }[/math] or [math]\displaystyle{ A \cdot B }[/math], or eliminating the multiplication sign altogether writing [math]\displaystyle{ AB }[/math]. However, the asterisk has a different meaning in programming. The Boolean and evaluates to true if and only if both operands are true.

A B A or B
false false false
false true false
true false false
true true true
truth table for logical conjunction

Bitwise operation

FPC also defines a bitwise and. Taking two ordinal operands logical and is calculated bit by bit:

    1010'1100
and 0011'0100
――――――――――――
    0010'0100

comparative remarks

Depending on the compiler's specific implementation of the data type set, the symmetric difference of sets >< virtually does the same as the bitwise and.

see also