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.

Examples of Bitwise And Or Operators


Let's see... examples of & and | operators... Well the classic example is option bits. Sometimes a programmer will use one integer number to contain several yes/no options. Each option is coded into one bit. These options are often called flags.

    ; turn on the "8" bit
    flags = flags | 8
When used this way, the bit values are always powers of 2, like 0, 1, 2, 4, 8, 16, 32, 64... But that's not very readable, so you often see

    ; turn on the "power" bit
    flags = flags | POWER
and "POWER" is a variable set to 8 someplace else in the program. By convention, variables like these are often in all UPPERCASE. These operations can be combined thusly:
   ; turn on light, camera and action
    flags = flags | LIGHTS | CAMERA | ACTION


  ;Where | combines bits, & strips away all but certain bits

    ; get value of camera flag bit
    value = flags & CAMERA

    ; test the lights
    If flags & LIGHTS Then Message("","lights are on")
Why, you ask, are the bits always powers of 2? It's all about "binary arithmetic". Do a google search and you'll probly find several decent tutorials. Very handy stuff. Hope this helps.
Article ID:   W16021
File Created: 2004:03:30:15:42:26
Last Updated: 2004:03:30:15:42:26