×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

AND Boolean

AND Boolean

AND Boolean

(OP)
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

RE: AND Boolean

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
http://newtonexcelbach.wordpress.com/

RE: AND Boolean

(OP)
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

RE: AND Boolean

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: Eng-Tips.com Forum Policies

RE: AND Boolean

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

RE: AND Boolean

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.

RE: AND Boolean

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.

RE: AND Boolean

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)' ?

RE: AND Boolean

"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: Eng-Tips.com Forum Policies

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login



News


Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close