Sign in to follow this  
Followers 0
Shane0000

Coding Question

5 posts in this topic

I want to be able to store some data as follows:

0 = nil

1 = data 1

2 = data 2

4 = data 3

8 = data 4

16 etc 

32 etc 

64 etc

So that I can 'turn on' bits 1 and 8 and use a value of 9 to represent bits 1 and 8 are on.

Is there an easy away to figure out what bits are on if I have a number such as 43 (showing that bits 32, 8, 2, and 1 are on)

I looked a bit into the bit functions, however im not sure on how they work or if it is what  am needing.

Thank you

Share this post


Link to post
Share on other sites



You'd use the BitAND function and test to see if a bit is set or not. Similar to the way that the _GUIToolTip_BitsToTTF UDF converts the bits set to text.

Func _GUIToolTip_BitsToTTF($iFlags)
    Local $iN = ""
    If BitAND($iFlags, $TTF_IDISHWND) <> 0 Then $iN &= "TTF_IDISHWND,"
    If BitAND($iFlags, $TTF_CENTERTIP) <> 0 Then $iN &= "TTF_CENTERTIP,"
    If BitAND($iFlags, $TTF_RTLREADING) <> 0 Then $iN &= "TTF_RTLREADING,"
    If BitAND($iFlags, $TTF_SUBCLASS) <> 0 Then $iN &= "TTF_SUBCLASS,"
    If BitAND($iFlags, $TTF_TRACK) <> 0 Then $iN &= "TTF_TRACK,"
    If BitAND($iFlags, $TTF_ABSOLUTE) <> 0 Then $iN &= "TTF_ABSOLUTE,"
    If BitAND($iFlags, $TTF_TRANSPARENT) <> 0 Then $iN &= "TTF_TRANSPARENT,"
    If BitAND($iFlags, $TTF_PARSELINKS) <> 0 Then $iN &= "TTF_PARSELINKS,"
    Return StringTrimRight($iN, 1)
EndFunc   ;==>_GUIToolTip_BitsToTTF

This tests to see if the $iFlags variable is set to any combination of different settings. The function checks for each possible setting and sets a text string identifying that that bit is set.

1 person likes this

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

More or less the same as above, with some added comments.

;

; Attention: Only assign powers of 2 up to 2^30 because, with 32-bit integers, 2^31 is out of range.

Local $iData0 = 2^0 ; = 1
Local $iData1 = 2^1 ; = 2
Local $iData2 = 2^2 ; = 4
Local $iData3 = 2^3 ; = 8
Local $iData4 = 2^4 ; = 16

; Set bits 1, 4 and 5
Local $iFlag = BitOR($iData0, $iData3, $iData4)

MsgBox(0, "Flag value = 25" , $iFlag = 25) ; Check the result

; Test which bits are set in the flag
If BitAND($iFlag, $iData0) Then MsgBox(0, "", "1st bit is set")
If BitAND($iFlag, $iData1) Then MsgBox(0, "", "2nd bit is set")
If BitAND($iFlag, $iData2) Then MsgBox(0, "", "3rd bit is set")
If BitAND($iFlag, $iData3) Then MsgBox(0, "", "4th bit is set")
If BitAND($iFlag, $iData4) Then MsgBox(0, "", "5th bit is set")

; Now let's remove $iData4 from the flag
$iFlag = BitXOR($iFlag, $iData4)

MsgBox(0, "New flag value = 9" , $iFlag = BitOR($iData0, $iData3)) ; Check the result

;

Fixed a mistake in comments.

Edited by czardas
1 person likes this

Share this post


Link to post
Share on other sites
....

So that I can 'turn on' bits 1 and 8 and use a value of 9 to represent bits 1 and 8 are on.

Is there an easy away to figure out what bits are on if I have a number such as 43 (showing that bits 32, 8, 2, and 1 are on)

I looked a bit into the bit functions, however im not sure on how they work or if it is what  am needing.

Thank you

 

Here are some examples of Bit functions to help you answer your questions.

Local $iNum = 43

ConsoleWrite("For " & $iNum & ":-" & @LF)
For $i = 0 To 32
    ;ConsoleWrite($i & "  0x" & hex(String(2 ^ $i),9) & @LF)
    If BitAND(2 ^ $i, $iNum) Then ConsoleWrite(" bit " & $i + 1 & ' is On ("1"). Equivalent decimal number is ' & BitAND(2 ^ $i, $iNum) & @LF)
Next
ConsoleWrite("BitOR(1, 2, 8, 32) = " & BitOR(1, 2, 8, 32) & @LF)
ConsoleWrite("----------------------------" & @LF)

ConsoleWrite($iNum & " " & DecToBin($iNum) & @LF)
ConsoleWrite(BitXOR($iNum, 2 ^ (2 - 1)) & " " & DecToBin(BitXOR($iNum, 2 ^ (2 - 1))) & " Toggle 2nd bit BitXOR" & @LF)
ConsoleWrite(BitOR($iNum, 2 ^ (3 - 1)) & " " & DecToBin(BitOR($iNum, 2 ^ (3 - 1))) & " Turn on 3rd bit BitOR" & @LF)
ConsoleWrite(BitAND($iNum, BitNOT(2 ^ (4 - 1))) & " " & DecToBin(BitAND($iNum, BitNOT(2 ^ (4 - 1)))) & " Turn 4th bit off - BitAND($iNum, BitNOT(2 ^ (4 - 1)))" & @LF)

Func DecToBin($dec)
    Local $temp
    While $dec >= 1
        $temp = Int(Mod($dec, 2)) & $temp
        $dec /= 2
    WEnd
    Return $temp
EndFunc   ;==>DecToBin

#cs
Results:-
For 43:-
 bit 1 is On ("1"). Equivalent decimal number is 1
 bit 2 is On ("1"). Equivalent decimal number is 2
 bit 4 is On ("1"). Equivalent decimal number is 8
 bit 6 is On ("1"). Equivalent decimal number is 32
BitOR(1, 2, 8, 32) = 43
----------------------------
43 101011
41 101001 Toggle 2nd bit BitXOR
47 101111 Turn on 3rd bit BitOR
35 100011 Turn 4th bit off - BitAND($iNum, BitNOT(2 ^ (4 - 1)))
#ce
1 person likes this

Share this post


Link to post
Share on other sites

Malkey, your code should fail because bitwise funtions don't work with 64-bit integers yet: 0x80000000 is -2^31, so setting the 32nd bit has to be done differently.

Share this post


Link to post
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
Sign in to follow this  
Followers 0