WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Miscellaneous

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Bitwise "AND"-ing and Flags Explained

Keywords:   bitwise and & wntUserGetDat flags binary extract bit bits decimal integer number 

Question:

I am trying to verify that the guest account is disabled on a list of servers. When I check the "flags" with wntUserGetDat, it returns a number (which I assume is all of the flags added together). Is there a way to know if the 2 flag is one of the flags that is set?

My current code isn't working because the GuestAcct variable is returning 531.

Here's the current code...

GuestAcct = wntUserGetDat("\\%CServer%", "Guest", "flags")
if GuestAcct == 2 
LOG16 = "CORRECT"
ELSE
LOG16 = "INCORRECT"
ENDIF
By reading some other postings and guessing, I got this to work by replacing the == with an &.

New Code

if GuestAcct & 2
Now... can you explain what the & symbol means here. What other instances would I use that?

(Can you tell that I don't have a programming background????)

Answer:

if GuestAcct & 2
...is indeed correct.

What's going on?

Under the covers all numbers (in WinBatch and *a lot* of other languages* consist of a chunk of 32 bits.

The normal arithmetic operation that you are used to ... + - * / work on these numbers and (usually) give you expected results. However there is another family of functions that can work not so much on the numbers as bits in the number.

A lot of "flag" type values use the individual bits instead of the numbers. So we are checking and clearing an individual one of these 32 bits. However, to make it even more confusing, Winbatch displays all these numbers in decimal, thus effectively hiding the nature of the bits underneath. You can use Windows Calculator in scientific mode to convert between decimal and binary to help see what is going on.

One sure-fire method to guess when bits instead of whole numbers are being used is to examine the flags values. If they are multiples of 2 then bits are being used, e.g.,

GuestAcct = wntUserGetDat("\\%CServer%", "Guest", "flags")
if GuestAcct & 2
In the example above, it got the GuestAcct flags and it is ANDing it with a decimal 2, which in binary is:
00000000 00000000 00000000 00000010
It's just got that one bit set and the AND operations is a kind of a bit by bit multiple. Both sides need to be set to get a non-zero answer when you're bitwise ANDing.
Article ID:   W14619
Filename:   Bitwise Anding Explained.txt
File Created: 2011:04:15:11:06:26
Last Updated: 2011:04:15:11:06:26