xHarbour Reference Documentation > Operator Reference xHarbour Developers Network  

& (bitwise AND)

Bitwise AND operator (binary): performs a logical AND operation.

Syntax

<cString> & <nMask>|<cMask> --> cCharacter

<nNumber> & <nMask>|<cMask> --> nNumeric

Arguments

<cString>
A character expression of which all bits of each character are processed in a logical AND operation.
<nNumber>
A numeric value all bits of which are processed. Numbers are always treated as integer values. If a number has a decimal fraction, it is truncated.
<nMask>|<cMask>
The right operand of the bitwise AND operator can be specified as a character or a numeric value.

Description

The bitwise AND operator performs a logical AND operation with the individual bits of both operands. The left operand is the value to process while the right operand provides the bit mask for the operation. The return value of the &-operator has the same data type as the left operand.

Bits at identical positions in both operands are compared. The bit at the same position is set in the return value, when both operands have the bit set at the same position. If either operand has a bit not set, the corresponding bit in the return value is also not set.

The bit mask to apply to the left operand can be specified as a numeric or as a character value. Depending on the left operand, bitwise AND operations are performed according to the following rules:

cString & cMask

When both operands are character values, the bits of individual characters of both operands are compared. If the right operand has less characters, it is repeatedly applied until all characters of the left operand are processed. The return value has the same number of characters as the left operand.

cString & nMask

When the left operand is a character value and the right operand is numeric, the bits set in the numeric value are compared with the bits of each character in the left operand.

nNumber & cMask

When the left operand is numeric and the right operand is a character value, the bits set in the numeric value are compared consecutively with the bits of each character in the right operand.

nNumber & nMask

When both operands are numeric values, the bits of both values are compared.

Note:  Numeric operands are always treated as integer values. If a number has a decimal fraction, it is ignored.

Info

See also:ˆˆ (bitwise XOR), | (bitwise OR), .AND., .NOT., .OR.
Category: Bitwise operators , Operators , xHarbour extensions
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example demonstrates bitwise AND operations using
// operands of different data types.

   #define  STAT_OFF    0
   #define  STAT_INIT   1
   #define  STAT_IDLE   2
   #define  STAT_ON     4

   PROCEDURE Main
      LOCAL nStatus := STAT_INIT + STAT_IDLE

      ? 2 & 3                          // result: 2

      ? "A" & 64                       // result: "@"

      ? 64 & "A"                       // result: 64

      ? "Z" & "A"                      // result: "@"

      ? nStatus & STAT_IDLE            // result: 2

      ? nStatus & STAT_ON              // result: 0

      ? "One" & "Two"                  // result: "Dfe"
   RETURN

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe