Table of Contents

RDC11005 — Bitwise operator

Severity: Hint  ·  Family: RDC (Rubberduck Core analyzers)  ·  Raised by: BitwiseSemanticsAnalyzer

A logical operator - And, Or, Xor, Eqv, Imp, Not - is given numbers rather than Boolean values, and so works on the bits of the numbers.

VBA has no separate logical and bitwise operators (MS-VBAL §5.6.9.8 Logical Operators). What an operator does depends on the type of its operands: two Boolean values give True or False, but two numbers are combined bit by bit, and the result is a number:

  • 1 And 2 is 0, although both 1 and 2 are "true" in an If condition: the two numbers have no bit in common.
  • Not 1 is -2, which is "true" too.

This is often exactly what is meant - reading a flag out of a set of flags, If (attributes And vbHidden) <> 0 Then - which is why this is a hint, not a warning. When it is not meant, the condition is true or false for reasons that have nothing to do with the values being "true" or "false".

How it is raised

Situation Example Resolution
A logical operator is given two numbers. If count And total Then, with count and total declared As Long Compare each number to what it is meant to be: If count <> 0 And total <> 0 Then.
A flag is read out of a number on purpose. If (attributes And vbReadOnly) <> 0 Then Nothing to change: the hint says what the operator does.

See also


ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca