Difference between revisions of "And"

From Lazarus wiki
Jump to navigationJump to search
(New page: And produces a value of true if and only if both of its operands are true. = Bitwise operation = == Clear a bit == <delphi> function ClearBit(const AValue, ABitNumber:integer):...)
 
(manually undo last changes by RfC1394: use categorization template {{and}} and fix syntax highlighting)
 
(21 intermediate revisions by 10 users not shown)
Line 1: Line 1:
And produces a value of [[True|true]] if and only if both of its operands are true.
+
{{And}}
  
= Bitwise operation =
+
The binary operator <syntaxhighlight lang="pascal" inline>and</syntaxhighlight> performs a logical conjunction.
 +
[[FPC]] also does a bitwise <syntaxhighlight lang="pascal" inline>and</syntaxhighlight> when supplied with ordinal types.
  
== Clear a bit ==
+
== Boolean operation ==
 +
The operator <syntaxhighlight lang="pascal" inline>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" inline>and</syntaxhighlight> evaluates to [[false and true|<syntaxhighlight lang="pascal" inline>true</syntaxhighlight>]] if and only if both operands are <syntaxhighlight lang="pascal" inline>true</syntaxhighlight>.
  
<delphi>
+
{| class="wikitable" style="text-align:center; margin:auto;"
function ClearBit(const AValue, ABitNumber:integer):integer;
+
! <syntaxhighlight lang="pascal" inline>A</syntaxhighlight>
begin
+
! <syntaxhighlight lang="pascal" inline>B</syntaxhighlight>
  result := AValue and not(1 shl ABitNumber);
+
! <syntaxhighlight lang="pascal" inline>A and B</syntaxhighlight>
end;
+
|-
</delphi>
+
| <syntaxhighlight lang="pascal" inline>false</syntaxhighlight>
If you call ClearBit(%1111,1) then get %1101 (%1111 = 15 and %1101 = 13). If you call ClearBit(13,2) then get 9 (9 = %1001) .
+
| <syntaxhighlight lang="pascal" inline>false</syntaxhighlight>
 +
| style="background: #eeeeee" | <syntaxhighlight lang="pascal" inline>false</syntaxhighlight>
 +
|-
 +
| <syntaxhighlight lang="pascal" inline>false</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>true</syntaxhighlight>
 +
| style="background: #eeeeee" | <syntaxhighlight lang="pascal" inline>false</syntaxhighlight>
 +
|-
 +
| <syntaxhighlight lang="pascal" inline>true</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>false</syntaxhighlight>
 +
| style="background: #eeeeee" | <syntaxhighlight lang="pascal" inline>false</syntaxhighlight>
 +
|-
 +
| <syntaxhighlight lang="pascal" inline>true</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>true</syntaxhighlight>
 +
| style="background: #eeeeee" | <syntaxhighlight lang="pascal" inline>true</syntaxhighlight>
 +
|+ truth table for logical conjunction
 +
|}
  
=== Read more ===
+
== Bitwise operation ==
* [[Not]]
+
FPC also defines a bitwise <syntaxhighlight lang="pascal" inline>and</syntaxhighlight>.
* [[Shl]]
+
Taking two ordinal operands logical <syntaxhighlight lang="pascal" inline>and</syntaxhighlight> is calculated bit by bit:
 +
    1010'1100
 +
and 0011'0100
 +
――――――――――――
 +
    0010'0100
  
* [[Const]]
+
== comparative remarks ==
* [[Function]]
+
Depending on the compiler's specific implementation of the data type [[Set|<syntaxhighlight lang="pascal" inline>set</syntaxhighlight>]], the [[Asterisk|intersection of sets]] virtually does the same as the bitwise <syntaxhighlight lang="pascal" inline>and</syntaxhighlight>.
* [[Integer]]
+
 
 +
== see also ==
 +
* [[Or|<syntaxhighlight lang="pascal" inline>or</syntaxhighlight>]]

Latest revision as of 15:57, 27 November 2022

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 and 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 intersection of sets virtually does the same as the bitwise and.

see also