Skip to content

Operator AND(合取)


返回两个数值的按位与(合取)。

语法

declare operator And ( byref lhs as T1, byref rhs as T2 ) as Ret

用法

result = lhs And rhs

参数

lhs

左侧表达式。

T1

任何数值或布尔类型。

rhs

右侧表达式。

T2

任何数值或布尔类型。

Ret

数值或布尔类型(随 T1T2 变化)。

返回值

返回两个操作数的按位与(合取)。

描述

此运算符返回其操作数的按位与,这是一种逻辑运算,根据操作数的位设置结果中的位(对于布尔值转换为整数,false 或 true 的布尔值变为 0 或 -1 的整数值)。

下面的真值表演示了布尔与运算的所有组合:

左操作数位右操作数位结果
000
100
010
111

不进行短路求值——两个表达式始终都会被求值。

返回类型取决于传入值的类型。ByteUbyte 和浮点类型的值首先转换为 Integer。如果左右操作数类型仅在有符号性上不同,则返回类型与左操作数类型(T1)相同,否则返回两种类型中较大的那个。只有当左右操作数类型都是 Boolean 时,返回类型才也是 Boolean

此运算符可以为用户定义类型重载。

示例

start GeSHi

vb
' Using the AND operator on two numeric values
Dim As UByte numeric_value1, numeric_value2
numeric_value1 = 15 '00001111
numeric_value2 = 30 '00011110

'Result =  14  =     00001110
Print numeric_value1 And numeric_value2
Sleep

end GeSHi

start GeSHi

vb
' Using the AND operator on two conditional expressions
Dim As UByte numeric_value1, numeric_value2
numeric_value1 = 15
numeric_value2 = 25

If numeric_value1 > 10 And numeric_value1 < 20 Then Print "Numeric_Value1 is between 10 and 20"
If numeric_value2 > 10 And numeric_value2 < 20 Then Print "Numeric_Value2 is between 10 and 20"
Sleep

' This will output "Numeric_Value1 is between 10 and 20" because
' both conditions of the IF statement is true
' It will not output the result of the second IF statement because the first
' condition is true and the second is false.

end GeSHi

方言差异

  • -lang qb 方言中,此运算符不能被重载。

与 QB 的差异

参见

返回 目录

基于 FreeBASIC 官方文档翻译 如有侵权请联系我们删除
FreeBASIC 是开源项目,与微软公司无隶属关系