Cool.

I have a couple of suggestions for your program.

Since you like binary, so much, I'm going to introduce you to your new best friends:

AND and

SHR :)

AND does a bit-wise comparison of two binary integers, and returns a binary integer which has ones in the positions where both of the first two numbers had ones. e.g.

Code: Select all

`dim as ubyte a = &b1010101, b = &b0001111, c`

c = a AND b

print bin(a, 8) '01010101

print bin(b, 8) '00001111

print bin(c, 8) '00000101

Now, you know that if "a MOD 2" is 1, i.e. if a is even, then a has a one at the end. So if you do "a AND &b00000001", or just "a AND 1", then answer will be 1 if a has a one at the end, and 0 if it has a zero at the end.

SHR takes a binary integer, e.g. &b01010111, and shifts it right, e.g. &b00101011. For example:

Code: Select all

`dim as ubyte a = &b11011010, b`

b = a SHR 3

print bin(a, 8) '11011010

print bin(b, 8) '00011011

You may have noticed that "a SHR n" is roughly equivalent to doing "int(a / 2^n)". The difference is, it's a LOT faster, because it doesn't have to do any floating point division. It just shifts the binary digits, which the computer is very good at.

Note, the opposite of

SHR is

SHL, which, obviously, shifts bits left, and is equivalent to multiplying by a power of 2.

Well, anyway, here's your code with the new functions:

Code: Select all

`Print "This program does the powers of .5(also negative powers of 2)."`

Print "How many do you want? Enter maximum exponent."

Input d

Cls

Dim a(d) As Ubyte

a(0)=1

For c=1 To d

For b=c To 0 Step-1

If (a(b) And 1) = 1 Then

a(b+1)=a(b+1)+5

End If

a(b)=a(b) Shr 1

Next

Print ".";

For b=1 To c

Print a(b);

Next

Print

Next