Jump to content

Out of Bounds BitShift - Solved


Recommended Posts

Even though it returns a result, the following code appears to be meaningless. The maximum possible shift is -31 to 31 (not -99999). Note: passing different out of bounds values for the shift parameter returns different results. I was wondering what was happening.

$a = BitShift(1, -99999)
MsgBox(0, "Meaningless", $a)
Edited by czardas
Link to comment
Share on other sites

The help file explains things clearly enough. Since it states that the function only works with numbers that fit in a 32 bit signed integer, I pressumed that shift values greater than 31 would produce out of bounds results. I really wanted to make sure I wasn't missing some important information (although I think I've got my head around it now) - in which case perhaps the Help file could mention something more about the shift parameter.

Edited by czardas
Link to comment
Share on other sites

So you're saying the help file is wrong?

I'm waiting for additional feedback before saying something like that. I don't really think the help file is wrong, just that some details appear undocumented.

Edit

Meh, the function seems to just simply be broken. It's not, it just seems to be. :whistle:

Edited by czardas
Link to comment
Share on other sites

To clarify my intentions: I am currently creating a small set of selected functions where all error checks are handled internally and do not need to be checked by the user. Each function will first check if an error has occured within a previously nested function. This is why I ask such questions (since I don't want to get this wrong), and here's what I have come up with for BitShift.

Global $_KickBack_ = 0 ; Count for nested function errors

$ret = _BitShift(Hex(15), 1) ; This nesting does not work with the native BitShift function.
MsgBox(0, @extended, $ret)

Func _BitShift($nValue, $nShift)
    If $_KickBack_ > 0 Then Return SetError(1, _HardError(), "NaN")

    If Not IsNumber($nValue) Then $nValue = Dec($nValue)
    If @error Then Return SetError(2, _HardError(), "NaN")

    If Not IsNumber($nShift) Then $nShift = Dec($nShift)
    If @error Then Return SetError(3, _HardError(), "NaN")

    If $nValue < -2147483648 Or $nValue >= 2147483648 Then Return SetError(4, _HardError(), "NaN")
    Return BitShift($nValue, $nShift) ; No native, or internal, error checks exist
EndFunc ;==>_BitShift

Func _HardError()
    $_KickBack_ += 1
    Return $_KickBack_
EndFunc
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

  • Recently Browsing   0 members

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