Sign in to follow this  
Followers 0
Luigi

CheckBin

7 posts in this topic

#1 ·  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
Script's data:
    Name:       CheckBin
    Version:    0.01
    Date:       15 may 2.011
Script's author
    Name:       Detefon
    e-mail:     herbivoro@ig.com.br

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 detefon

m(o.O)m

Share this post


Link to post
Share on other sites



#2 ·  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

Share this post


Link to post
Share on other sites

"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)

Share this post


Link to post
Share on other sites

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

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

I must be getting rusty if I had forgotten that. Thanks for reminding me, though. :unsure:

Share this post


Link to post
Share on other sites

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.


m(o.O)m

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

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