Jump to content

Recommended Posts

Posted (edited)

My first script in the forum.

This is a simple code to verify if a number is contained in binary number.

#comments-start

This script check if $Inside is contained in $Binary.
17 = 1 + 16
_CheckBin(17,15)    = 0
_CheckBin(17,1)     = 1
_CheckBin(17,3)     = 0
_CheckBin(17,16)    = 1


#comments-end
#include <Array.au3>
ConsoleWrite(_CheckBin(17,15))

Func _CheckBin($Binary,$Inside)
    Dim $BinTable[1]
    Local $BinOriginal, $BinTable
    If $Binary <0 or $Inside<0 Then Return 0
    If $Inside > $Binary Then Return 0

    $BinRest=1
    $BinExp = 0
    $BinarioString=""

While $Binary >0.5
    $BinRest = _Rest($Binary,2)
    $Binary = int($Binary/2)
    $BinString = $BinarioString & $BinRest
    ReDim $BinTable[$BinExp+1]
    
    IF $BinRest = 1 Then
        $BinOriginal = $BinOriginal + 2^($BinExp)
        $BinTable[$BinExp] = 2^($BinExp)
    Endif

    $BinExp = $BinExp + 1
WEnd

_ArraySearch($BinTable, $Inside, 0, 0, 0, 0,0)
    If @error Then
        Return 0
    Else
        Return 1
    EndIf
EndFunc     ; _CheckBin()

Func _Rest($Dividend,$Divider)
    Return (($Dividend / $Divider)-int(($Dividend / $Divider)))*2
EndFunc     ; _Rest()

CheckBin02.au3

Edited by Luigi

Visit my repository

Posted (edited)

"verify if a number is contained in binary number" ?

I think you are attempting to describe the following bitwise operation:

return (BitOR($whole, $part) = $whole)
; edited according to comment below
; replaced == with = comparison operator

which could also be written:

return NOT(BitXOR($whole, BitOR($whole, $part)))
Edited by Sapient
Posted

"verify if a number is contained in binary number" ?

I think you are attempting to describe the following bitwise operation:

return (BitOR($whole, $part) == $whole)

Why are you doing a string comparison (see "Operators" in helpfile)? I think you meant:

ConsoleWrite((BitOR(17, 15) = 17) & @CRLF)
Posted

I hate me! grrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr

I lost my time writing this thing... and they have a solution in 1 code line!

I hate me! grrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr

8(

Anyway... thanks (@Sapient and @AdmiralAlkex)for your reply, is very useful for me.

Visit my repository

Posted

I lost my time writing this thing... and they have a solution in 1 code line!

True, but my solution only works for binary numbers that are up to 32 bits.

If you want to compare larger binaries (e.g. graphics files) a special function could still be needed.

Posted

I hate me! grrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr

I lost my time writing this thing... and they have a solution in 1 code line!

I hate me! grrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr

8(

Anyway... thanks (@Sapient and @AdmiralAlkex)for your reply, is very useful for me.

Don't beat yourself up dude! Everybody has to start somewhere.

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
  • Recently Browsing   0 members

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