Jump to content

bitwise calcs


Recommended Posts

I wonder if anyone could help me understand how to use them/ how they work

I never even heard of then before landing on this forum :mellow:

I'll give an example using these flags

1

2

4

8

16

32

64

If I have the number 44, how do I use the bitwise functions to decide what flags are used to arrive at that number.

It looks like binary and Im one of the 0 people who understands it.

Help much appreciated.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

  • Moderators

JohnOne,

You are correct - it is binary. Are you sitting comfortably? Then let us begin....

First AND and OR. An AND operation is only true if BOTH elemnts have the bit set. An OR operation is true if EITHER of the elements have the bit set:

0010
AND
    0111 will give 0010 because only that bit is set in BOTH

    0010
OR
    0111 will give 0111 because those bits are set in EITHER one of the elements

Ther is also XOR, which is true only if the bits are different in each element :P - but we will not go there today!

Look at those flags you mentioned in binary (remember we go up in powers of 2:

Power of 2   7   6   5   4   3   2   1   0

Value        128 64  32  16  8   4   2   1

1                                        x  = binary 00000001
2                                    x      = binary 00000010
4                                x          = binary 00000100
...
64               x                          = binary 01000000

And

44                   x       x   x          = 32 + 8 + 4 = binary 00101100

So in essence you have to check whether a particulr bit is set in the number - gets complicated!

But AutoIt uses the Bit* functions to help you use the decimal numbers that we (or at least some of us) are more used to dealing with. So to see if the flags 32, 8 and 4 are set we use BitAND:

$iMask = 44 ; 00101100

For $i = 43 To 45
    If BitAnd($i, $iTest) = $iMask Then ConsoleWrite($i & " matches!" & @CRLF)
Next

Note that we match both 44 and 45 as the flags are set in both cases:

43 = 00101011 = __X_X_xx = not all our flags set
44 = 00101100 = __X_XX__ = all our flags set
45 = 00101101 = __X_XX_x = all our flags set - even though there are others also set!

Remember we are looking at individual bits and not the whole number. If we wanted to do that we could just use If $i = $iMask. :mellow:

We use BitOR to add styles for a similar reason. It makes sure that only the bits associated with that particular style are set. If we added the actual values of the styles together we could end up with a number which set the wrong bits:

Style 1 = 17 = 00010001
Style 3 = 33 = 00100001

If we add we get 50: 00110010 and the flag bit are wrongly set
If we BitOR we get:  00110001 and the flag bits are correctly set

See the difference? Most of the time there is not a problem - but it best to use BitOr to be sure.

Is that enough to be going on with? :party:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I wrote also a converter where you can enter binary or integer value which will be converted appropriately to the other format.

Integer <=> Binary Converter

BR,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Don't forget you have a nice calculator doing most usual conversion for you: the Windows calculator. Just don't forget to switch to Scientific display to have access to numeric conversions between any two of decimal, hexadecimal, octal and binary for 8-, 16-, 32- or 64-bit wide values.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...