Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

AND Boolean

Status
Not open for further replies.

haemon17

Structural
Dec 1, 2011
5
I'm trying to understand what AND is doing with the integers in the following code. Both a1 and b1 evaluate to TRUE but c1 evaluates to FALSE. Something similar to this code was in a spreadsheet made by someone else.

Any ideas on why some integers return c1=TRUE and others return c1=FALSE??

Sub testand()
Dim a1 As Integer
Dim b1 As Integer
Dim c1 As Boolean
a1 = 12
b1 = 2
c1 = (a1 And b1)
Sheet1.Range("E2").Value = c1
Sheet1.Range("E3").Value = CBool(a1)
Sheet1.Range("e4").Value = CBool(b1)
End Sub

 
Replies continue below

Recommended for you

It seems that individual integers greater than zero evaluate to TRUE, but two integers with an AND evaluate to false.

c1 = ((a1 > 0) And (b1 > 0))

will work.

Doug Jenkins
Interactive Design Services
 
The thing is that not all integer combination evaluate to False

5 AND 2 = False
5 AND 8 = False
5 AND 10 = False

but

5 AND (1,3,4,5,6,7,&9) all evaluate to True
 
VBA Help explains that logical AND is based on a bitwise ANDing of numbers, if entered. Then, presumably, the bits are OR'd together. Therefore, any number which has a binary 4 or 1 to match the bits set in an integer 5, will result in a True, whereas, those that have no matching bits must all evaluate to 0 and thence, FALSE.

TTFN
faq731-376
7ofakss
 
I've been looking at this but haven't been able to figure it out yet, but I don't have any more time to work on it now. I wrote the code below to help me. Maybe it will be helpful to someone else who wants to figure this out. The first procedure is just the code provided by haemon modified to place it in a different range on the worksheet. The second is my evaluation tool. I think you'll see what I was doing after you run the macro.

Sub testand()
Dim a1 As Integer
Dim b1 As Integer
Dim c1 As Boolean
a1 = 12
b1 = 2
c1 = (a1 And b1)
Sheet1.Range("g2").Value = c1
Sheet1.Range("g3").Value = CBool(a1)
Sheet1.Range("g4").Value = CBool(b1)
End Sub

Sub test()

For counter = 0 To 1000
Range("a2").Offset(counter, 0).Value = -500 + counter
Range("a2").Offset(counter, 1).Value = CBool(Range("a2").Offset(counter, 0))

Range("c2").Offset(counter, 0).Value = -510 + counter
Range("c2").Offset(counter, 1).Value = CBool(Range("c2").Offset(counter, 0))

Range("e2").Offset(counter, 0).Value = (Range("e2").Offset(counter, -3).Value And Range("e2").Offset(counter, -1).Value)
Range("f2").Offset(counter, 0).Value
Next
End Sub
 
As pointed out above, when AND'ing two integers VBA does it bitwise, producing an integer result.

When it comes to convert that integer to a boolean, it takes the view that an integer of zero corresponds to FALSE, and non-zero corresponds to TRUE.
 
Boolean truth varies depending on what language you are using. In some languages (like C)

0 = false
anything else = true

In some languages

0 = false
1 = true
anything else is undefined

In some languages

0 = false
all bits set = true
anything else is undefined

In some languages (eg P/LM), only the Least Significant bit is checked.

0 = false
1 = true

So 4 (100) would be false and 5 (101) would be true.
 
"Boolean truth varies"

No, it doesn't. Incorrect/inappropriate implementation of boolean varies.

TTFN
faq731-376
7ofakss
 
I would say the most relevant aspect to understand during programming is how the program you're using responds to arguments other than 1 and 0 and xwb makes a useful point about how it varies.


=====================================
(2B)+(2B)' ?
 
"how the program you're using responds to arguments other than 1 and 0"

No argument there; but that applies to EVERYTHING that takes an input. This is the source of a multitude of buffer overrun exploits that have plagued many Microsoft programs.

TTFN
faq731-376
7ofakss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor