Difference between revisions of "And"

From Free Pascal wiki
Jump to navigationJump to search
(15 intermediate revisions by 8 users not shown)
Line 1: Line 1:
= Boolean operation =
+
{{And}}
  
'''And''' produces a value of [[True|true]] if and only if both of its operands are true.
+
The binary operator {{HL|and}} performs a logical conjunction.
 +
[[FPC]] also does a bitwise {{HL|and}} when supplied with ordinal types.
  
== Truth table ==
+
== Boolean operation ==
 +
The operator {{HL|and}} 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 {{HL|and}} evaluates to [[false and true|{{HL|true}}]] if and only if both operands are {{HL|true}}.
  
{| border="1" style="border: 1px  solid; border-collapse: collapse;"
+
{| class="wikitable" style="text-align:center; margin:auto;"
 +
! {{HL|A}}
 +
! {{HL|B}}
 +
! {{HL|A or B}}
 
|-
 
|-
!align=center| A !! align=center|B !! &nbsp; A and B &nbsp;
+
| {{HL|false}}
 +
| {{HL|false}}
 +
| style="background: #eeeeee" | {{HL|false}}
 
|-
 
|-
| &nbsp; false &nbsp;|| &nbsp; false &nbsp;
+
| {{HL|false}}
|style="background: #eeeeee" | &nbsp; false
+
| {{HL|true}}
 +
| style="background: #eeeeee" | {{HL|false}}
 
|-
 
|-
| &nbsp; false || &nbsp; true
+
| {{HL|true}}
|style="background: #eeeeee" | &nbsp; false
+
| {{HL|false}}
 +
| style="background: #eeeeee" | {{HL|false}}
 
|-
 
|-
| &nbsp; true || &nbsp; false
+
| {{HL|true}}
|style="background: #eeeeee" | &nbsp; false
+
| {{HL|true}}
|-
+
| style="background: #eeeeee" | {{HL|true}}
| &nbsp; true || &nbsp; true  
+
|+ truth table for logical conjunction
|style="background: #eeeeee" | &nbsp; true
 
 
|}
 
|}
  
 +
== 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
  
= Bitwise operation =
+
== comparative remarks ==
 
+
Depending on the compiler's specific implementation of the data type [[Set|{{HL|set}}]], the [[Asterisk|intersection of sets]] virtually does the same as the bitwise {{HL|and}}.
Bitwise and sets a bit to 1 if and only if all of the corresponding bits in its operands are 1.
 
 
 
== Clear a bit ==
 
 
 
<delphi>
 
function ClearBit(const AValue, ABitNumber:integer):integer;
 
begin
 
  result := AValue and not(1 shl ABitNumber);
 
end;
 
</delphi>
 
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) .
 
  
=== Read more ===
+
{{Logical operators}}
* [[Not]]
 
* [[Shl]]
 
  
* [[Const]]
+
[[Category:Pascal]]
* [[Function]]
+
[[Category:Operators]]
* [[Integer]]
 

Revision as of 18:27, 15 October 2020

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


navigation bar: Pascal logical operators
operators

and • or • not • xor
shl • shr
and_then (N/A)• or_else (N/A)

see also

{$boolEval} • Reference: § “boolean operators” • Reference: § “logical operators”