Jump to content

[SOLVED] How to "Zero-fill right shift" ?


FireFox
 Share

Recommended Posts

Hi,

I'm wondering how to make a zero-fill right shift in autoit as far as the BitShift function has no parameter for this.

Here is an example in javascript : https://developer.mozilla.org/en-US/docs/Javascript/Reference/Operators/Bitwise_Operators#>>>_(Zero-fill_right_shift)

Thanks for anyhelp,

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

Perhaps something like this:

For $i = 0 To 32
    ConsoleWrite(LogicalBitShift(0x80000000 ,$i) & @LF)
Next

Func LogicalBitShift($dVal, $iShift)
    If $iShift > 0 And BitAND($dVal, 0x80000000) = 0x80000000 Then
        $dVal = BitShift($dVal, 1)
        $dVal = BitXor($dVal, 0x80000000)
        $iShift -= 1
    EndIf
    Return BitShift($dVal, $iShift)
EndFunc
Edited by czardas
Link to comment
Share on other sites

Nice snippet czardas

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

This is a equivalent to the LogicalBitShift() function:

For $i = 0 To 32
   ConsoleWrite(Int(-1 * 0x80000000 / (2^$i)) & @LF)
Next

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

This is a equivalent to the LogicalBitShift() function:

For $i = 0 To 32
Ceiling(-1 * 0x80000000 / (2^$i))
Next

Br,

UEZ

I'm getting totally different results:

Local $dVar = 0xFFFFFFFF

For $i = 0 To 32
    ConsoleWrite(BitLogicalShift($dVar ,$i) & @LF)
    ConsoleWrite(Ceiling(-1 * $dVar / (2^$i)) & @LF)
Next

Func BitLogicalShift($dVal, $iShift)
    If $iShift > 0 And BitAND($dVal, 0x80000000) = 0x80000000 Then
        $dVal = BitShift($dVal, 1)
        $dVal = BitXor($dVal, 0x80000000)
        $iShift -= 1
    EndIf
    Return BitShift($dVal, $iShift)
EndFunc

BTW, I changed the function name to a better one, but there is a problem. I'm trying to figure it out.

Edited by czardas
Link to comment
Share on other sites

Well, calculating this way is limited to 0x80000000.

Local $dVar = 0x80000000

For $i = 0 To 32
    ConsoleWrite(BitLogicalShift($dVar, $i) & " / " & Equivalent($dVar, $i) & @LF)
Next

Func Equivalent($dVar, $i)
    If Hex($dVar) > Hex(0x80000000) Then Return SetError(0, 0, -1)
    Local $j = 1
    If Hex($dVar) = Hex(0x80000000) Then $j *= -1
    Return Int($j * $dVar / (2^$i))
EndFunc

Func BitLogicalShift($dVal, $iShift)
    If $iShift > 0 And BitAND($dVal, 0x80000000) = 0x80000000 Then
        $dVal = BitShift($dVal, 1)
        $dVal = BitXor($dVal, 0x80000000)
        $iShift -= 1
    EndIf
    Return BitShift($dVal, $iShift)
EndFunc

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

Well, it has to work on any 32 bit integer. I have just realized that my method is faulty, because it isn't a true logical shift. The results of the first example fooled me into thinking it was working. It's actually completely broken.

Edit

I think I know a way to fix it.

Edited by czardas
Link to comment
Share on other sites

I was thinking way too deep about this, The solution is very simple indeed. I have added error checks to force strict input parameters, rather unlike the arithmetic bitwise functions which don't seem to balk at anything. :ph34r:

Local $dVar = 0x00010000

For $i = 21 To -20 Step -1
    ConsoleWrite(BitLogicalShift($dVar ,$i) & @LF)
Next

Func BitLogicalShift($dVal, $iShift)
    If Not IsInt($dVal) Or Not IsInt($iShift) Then Return SetError(1, 0, "")

    If $iShift > 31 Or $iShift < -31 Then
        $dVal = 0 ; Out of range shift values always return zero.
    ElseIf $iShift > 0 And BitAND($dVal, 0x80000000) = 0x80000000 Then
        $dVal = BitXor(BitShift($dVal, 1), 0x80000000)
        $iShift -= 1
    EndIf

    Return BitShift($dVal, $iShift)
EndFunc

Now the bit sequence can be shifted out of range in either direction. With the earlier version, the binary was looping back on itself with shift values greater than 31. That was not the intention.

Edited by czardas
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

×
×
  • Create New...