Welcome Guest
Log In | Register )
You last visited May 24, 2017, 12:10 am
All times shown are
Eastern Time (GMT-5:00)

# Binary code

Topic closed. 15 replies. Last post 7 years ago by frenchie.

 Page 1 of 2
PrintE-mailLink
Thread Starter
Los Angeles
United States
Member #75410
June 2, 2009
502 Posts
Offline
 Posted: June 2, 2010, 7:21 pm - IP Logged

Hi all,

I'm not sure but I think that the Binary code  below has an error,can someone test it and correct it if it's not correct,because when I enter the number 25 it give me : 11001 et when I put 50 it give me the same : 11001 !!?

Thank you.

Function DecBin\$(V As Variant)

Dim r\$, L&, i%, K&

r = ""
If IsNumeric(V) Then
L = Val(V)
i = 16
While 2 ^ i > L
i = i - 1
Wend
' Now we have an i that is either equal to L or less.
If 2 ^ i = L Then
r = "1" & String(i, "0")
Else
r = "1"
K = 2 ^ i
While K <> L
i = i - 1
If K + 2 ^ i <= L Then
K = K + 2 ^ i
r = r & "1"
Else
r = r & "0"
End If
Wend
End If
End If

DecBin = r

End Function

France
Member #92435
June 7, 2010
43 Posts
Offline
 Posted: June 8, 2010, 4:46 am - IP Logged

Hi,

Well the number 25 in binary is 11001 and the number 50 in binary is 110010 so I think you may have an error in your array sizes.

Rather than correcting your VB code I'm posting it in php maybe you can "translate" it to VB, I'm using recusivity that makes it easy:

/**
* binary function by themagician
* it takes an integer and print its binary value
* June - 2010
*
*/

// It prints: 11001
binary(25);
print "\n";
// It prints: 110010
binary(50);

function binary(\$number)
{
\$remainder;

if(\$number <= 1) {
print \$number;
return;
}
// Recursivity until the result of the division is zero
\$remainder = \$number%2;
binary(\$number >> 1);
print \$remainder;
}

I hope it helps ;-)

Imagination can take you anywhere

United States
Member #59354
March 13, 2008
4301 Posts
Offline
 Posted: June 9, 2010, 9:08 am - IP Logged

Hi all,

I'm not sure but I think that the Binary code  below has an error,can someone test it and correct it if it's not correct,because when I enter the number 25 it give me : 11001 et when I put 50 it give me the same : 11001 !!?

Thank you.

Function DecBin\$(V As Variant)

Dim r\$, L&, i%, K&

r = ""
If IsNumeric(V) Then
L = Val(V)
i = 16
While 2 ^ i > L
i = i - 1
Wend
' Now we have an i that is either equal to L or less.
If 2 ^ i = L Then
r = "1" & String(i, "0")
Else
r = "1"
K = 2 ^ i
While K <> L
i = i - 1
If K + 2 ^ i <= L Then
K = K + 2 ^ i
r = r & "1"
Else
r = r & "0"
End If
Wend
End If
End If

DecBin = r

End Function

Got off the net years ago works well, sorry for the format

FUNCTION DecBin\$ (Decimal&) STATIC
BinNum\$ = ""
H\$ = HEX\$(Decimal&)
FOR I% = 1 TO LEN(H\$)
DIGIT% = INSTR("0123456789ABCDEF", MID\$(H\$, I%, 1)) - 1
IF DIGIT% < 0 THEN
BinNum\$ = ""
EXIT FOR
END IF
J% = 8
K% = 4
DO
BinNum\$ = BinNum\$ + RIGHT\$(STR\$((DIGIT% \ J%) MOD 2), 1)
J% = J% - (J% \ 2)
K% = K% - 1
IF K% = 0 THEN
EXIT DO
END IF
LOOP WHILE J%
NEXT I%
DecBin\$ = BinNum\$
END FUNCTION

Hope this helps

LP

....

United States
Member #59354
March 13, 2008
4301 Posts
Offline
 Posted: June 13, 2010, 1:15 am - IP Logged

Hi all,

I'm not sure but I think that the Binary code  below has an error,can someone test it and correct it if it's not correct,because when I enter the number 25 it give me : 11001 et when I put 50 it give me the same : 11001 !!?

Thank you.

Function DecBin\$(V As Variant)

Dim r\$, L&, i%, K&

r = ""
If IsNumeric(V) Then
L = Val(V)
i = 16
While 2 ^ i > L
i = i - 1
Wend
' Now we have an i that is either equal to L or less.
If 2 ^ i = L Then
r = "1" & String(i, "0")
Else
r = "1"
K = 2 ^ i
While K <> L
i = i - 1
If K + 2 ^ i <= L Then
K = K + 2 ^ i
r = r & "1"
Else
r = r & "0"
End If
Wend
End If
End If

DecBin = r

End Function

frenchie

I had a little time and looked over your code.  When the number to convert is a odd number your code

will work correct but if it is even then it will be missing the lsd.  Check any even number and it will be missing

the "0" digit at the end of the string.  25 binary is 11001 and 50 binary is 110010. Your code is terminating

early with all even numbers because the lsd is zero.  This is a easy fix.

RL

....

Thread Starter
Los Angeles
United States
Member #75410
June 2, 2009
502 Posts
Offline
 Posted: June 13, 2010, 4:06 am - IP Logged

Thank you themagician and RL-RANDOMLOG,

your answers were very appreciated, thanks again that helped alot.

United States
Member #59354
March 13, 2008
4301 Posts
Offline
 Posted: June 14, 2010, 12:58 am - IP Logged

frenchie

I had a little time and looked over your code.  When the number to convert is a odd number your code

will work correct but if it is even then it will be missing the lsd.  Check any even number and it will be missing

the "0" digit at the end of the string.  25 binary is 11001 and 50 binary is 110010. Your code is terminating

early with all even numbers because the lsd is zero.  This is a easy fix.

RL

frenchie

Look at this and It should help you see your error and correct your code

l=25 ' number to convert to decimal

i = 16  allows function to convert any decimal <=65536

(The long version)

1=16

2^i=65536  is >25

i=i-1 'i=15

2^i=32768  is >25

i=i-1 'i=14

2^i=16384  is > 25

i=i-1 'i=13

2^i=8192   is > 25

i=i-1 'i=12

2^i=4096   is > 25

i=i-1 'i=11

2^i=2048   is > 25

i=i-1 'i=10

2^i=1024   is > 25

i=i-1 'i=9

2^i=512    is > 25

i=i-1 'i=8

2^i=256   is > 25

i=i-1 'i=7

2^i=128   is > 25

i=i-1  'i=6

2^i=64   is > 25

i=i-1 'i=5

2^i=32   is > 25

i=i-1 'i=4

2^i=16  16 <=25

here you now have (2 ^ i  <=  l),    (2 ^ 4 = 16) and  (16 <= 25)  so r = "1"

You now have the MSD for the binary string.  From this point, convert the

remaining digits; one digit at a time using the " l = l - i " each time (2 ^ i  <=  l)

I left this undone  for you to finish.

example steps to compleat

l = l - i

while i <= 0

i = i - 1

if 2 ^ i <= l then r = r & "1"  Else r = r & "0"

if 2 ^ i <= l then l = l - i

Next

RL

....

United States
Member #59354
March 13, 2008
4301 Posts
Offline
 Posted: June 14, 2010, 4:37 pm - IP Logged

frenchie

Look at this and It should help you see your error and correct your code

l=25 ' number to convert to decimal

i = 16  allows function to convert any decimal <=65536

(The long version)

1=16

2^i=65536  is >25

i=i-1 'i=15

2^i=32768  is >25

i=i-1 'i=14

2^i=16384  is > 25

i=i-1 'i=13

2^i=8192   is > 25

i=i-1 'i=12

2^i=4096   is > 25

i=i-1 'i=11

2^i=2048   is > 25

i=i-1 'i=10

2^i=1024   is > 25

i=i-1 'i=9

2^i=512    is > 25

i=i-1 'i=8

2^i=256   is > 25

i=i-1 'i=7

2^i=128   is > 25

i=i-1  'i=6

2^i=64   is > 25

i=i-1 'i=5

2^i=32   is > 25

i=i-1 'i=4

2^i=16  16 <=25

here you now have (2 ^ i  <=  l),    (2 ^ 4 = 16) and  (16 <= 25)  so r = "1"

You now have the MSD for the binary string.  From this point, convert the

remaining digits; one digit at a time using the " l = l - i " each time (2 ^ i  <=  l)

I left this undone  for you to finish.

example steps to compleat

l = l - i

while i <= 0

i = i - 1

if 2 ^ i <= l then r = r & "1"  Else r = r & "0"

if 2 ^ i <= l then l = l - i

Next

RL

frenchie

I made a mistake shown in bold

here you now have (2 ^ i  <=  l),    (2 ^ 4 = 16) and  (16 <= 25)  so r = "1"

You now have the MSD for the binary string.  From this point, convert the

remaining digits; one digit at a time using the " l = l - i " each time (2 ^ i  <=  l)

I left this undone  for you to finish.                        |_ above should be l = l - 2 ^ i  not l = l - i

example steps to compleat

l = l - i

while i <= 0

i = i - 1

if 2 ^ i <= l then r = r & "1"  Else r = r & "0"

if 2 ^ i <= l then l = l - i <--------this should be  l = l - (2 ^ i)

sorry

RL

....

United States
Member #59354
March 13, 2008
4301 Posts
Offline
 Posted: June 14, 2010, 5:14 pm - IP Logged

frenchi3

ignore the above I have made so many typeo's as in the while i <= 0 should be While i >= 0

I was trying to give you an idea where your mistake was without pointing it out directly. Allowing

you to solve it.

Here is a basic code example the only difference it prints all 16 bits.  I will stop playing the "stupid me"

input "enter number to convert ", L

R\$ = "": i=16:
DO WHILE I >= 0
IF 2 ^ I <= L THEN R\$ = R\$ + "1" ELSE R\$ = R\$ + "0"
IF 2 ^ I <= L THEN L = (L - 2 ^ I)
I = I - 1
LOOP

PRINT ""; R\$; ""

RL

....

Thread Starter
Los Angeles
United States
Member #75410
June 2, 2009
502 Posts
Offline
 Posted: June 14, 2010, 6:49 pm - IP Logged

Hi-RANDOMLOG,

Thank you for your response, but you're talking Chinese to me, I do not know anything about macro, someone wrote that one for me and ready to use in an Excel file.

I realy do appreciate your effort, and admire your knolege I wiche I could do the same but not today,

The reason why I needed a binary code was to play and transfer number into it to see what I can came up with, you never know always try, my things is to work with patterns so, that could lead to something interressting ? But what would realy help me if you can is to post this excel binary file ready to use through boxnet and i'm sure other member would appreciate it too

but I do need help with formula and conditional formatting, maybe you can help me with that ?

Thanks HI-RANDOMLOG.

France
Member #92435
June 7, 2010
43 Posts
Offline
 Posted: June 16, 2010, 8:41 am - IP Logged

Hi-RANDOMLOG,

Thank you for your response, but you're talking Chinese to me, I do not know anything about macro, someone wrote that one for me and ready to use in an Excel file.

I realy do appreciate your effort, and admire your knolege I wiche I could do the same but not today,

The reason why I needed a binary code was to play and transfer number into it to see what I can came up with, you never know always try, my things is to work with patterns so, that could lead to something interressting ? But what would realy help me if you can is to post this excel binary file ready to use through boxnet and i'm sure other member would appreciate it too

but I do need help with formula and conditional formatting, maybe you can help me with that ?

Thanks HI-RANDOMLOG.

Ah, looking for patterns in binary code, interesting,... that reminds me of the latest discoveries in the England crop fields circles. You know, those circles that appear avery now and then on the fields and nowone knows how they've been done!

One of those circles was full of small lines that someone saw as binary code, then they translated to ascii values and they got an Euler formula!

Imagination can take you anywhere

United States
Member #59354
March 13, 2008
4301 Posts
Offline
 Posted: June 17, 2010, 5:23 am - IP Logged

frenchie

copy and paste this code and see if it works, may need to remove the "End IF"

I = 7 so it will convert any number <= 255 can be changed if needed

prints all 8 bits format = "01000101"

DECLARE FUNCTION DecBin\$ (V AS ANY)
FUNCTION DecBin\$ (V AS Variant)
DIM R\$, L%, I%
I = 7
R = ""
IF IsNumeric(V) THEN
L = VAL(V)

WHILE I >= 0
IF 2 ^ I <= L THEN R = R & "1" ELSE R = R & "0"
IF 2 ^ I <= L THEN L = L - 2 ^ I
I = I - 1
WEND

End IF

DecBin = R

END FUNCTION

....

Thread Starter
Los Angeles
United States
Member #75410
June 2, 2009
502 Posts
Offline
 Posted: June 17, 2010, 12:11 pm - IP Logged

Hi RANDOMLOG,

Thank you for the code, but could you help me step by step how to make it work on the excel file, I don't know nothing about macro, that would be a good lesson for me.

Thank you.

United States
Member #59354
March 13, 2008
4301 Posts
Offline
 Posted: June 17, 2010, 4:09 pm - IP Logged

Hi RANDOMLOG,

Thank you for the code, but could you help me step by step how to make it work on the excel file, I don't know nothing about macro, that would be a good lesson for me.

Thank you.

frenchie

I am a little confused.  How and with what, were you using the code above to get the problems listed

in this post.  Macro is not hard to learn and you can find help online just google"macro & tutorial"  and

you should find all the help you need.  I don't have time to do this, sorry.  You mentioned looking for

patterns in binary, I have done much in this area and have found little that could help with the lottery

but don't let this detain you.

You first may need to learn some vb programming since this seems to be outside your current abilities.

This too can also be found online.  Just take your time, Rome wasen't built in a day.  You can even find

video tutorials if reading is not your best method of learning, go to youtube and search. You may not

find an exact match for what you are doing but as with all language's  once you learn the basics you

are on your way. I have been doing this over 20 years and still learn something new most every day.

I always code new ideas in basic first as it allows for very fast builds. It also allows me to test an idea

without having much time invested.

RL

....

Thread Starter
Los Angeles
United States
Member #75410
June 2, 2009
502 Posts
Offline
 Posted: June 29, 2010, 2:23 pm - IP Logged

Thanks RL for your answer but I'm not interrested in learning macro, it is too complicated for me and I don't need them much for my lottery study that's why I ask you guys help, I work more with formula.

Thank you again RL.              Frenchie.

Thread Starter
Los Angeles
United States
Member #75410
June 2, 2009
502 Posts
Offline
 Posted: July 2, 2010, 4:30 pm - IP Logged

Hi,

If someone is interested in binary formula here is what I find 3 diferentes one.

Put your number in cell A1 and the formula in B1.

1 . for A Binary of 12 digits the formula is :

=DEC2BIN((MOD(A1,4096)/512),3) & DEC2BIN(MOD(A1,512),9)

The maximum number you can go with this one is : 4 095.

2 . for A Binary of 18 digits the formula is : =CONCATENATE(DEC2BIN(INT(A1/512),9),DEC2BIN(MOD(A1,512),9))

The maximum number you can go with this one is : 262 143.

3 . for A Binary of 18 digits the formula is :

=DEC2BIN((MOD(A1,4294967296)/16777216),8) & DEC2BIN(MOD(A1,16777216)/65536,8) & DEC2BIN(MOD(A1,65536)/256,8) & DEC2BIN(MOD(A1,256),8)

The maximum number you can go with this one is : 34 359 738 368.

I hope it will help some one if you need it ?

Frenchie.

 Page 1 of 2