Hey,this is a really cool trick with negative powers of 2!

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
2
Posts: 75
Joined: Mar 28, 2006 21:01

Hey,this is a really cool trick with negative powers of 2!

Postby 2 » Apr 02, 2006 22:02

I am VERY happy with this program.I'm not asking help with it.
I just think it's cool,and I'm trying 2 indent it.
It's pretty easy 2 see what it does.

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) Mod 2=1 Then
            a(b+1)=a(b+1)+5
        End If
        a(b)=Int(a(b)/2)
    Next
    Print ".";
    For b=1 To c
        Print a(b);
    Next
    Print
Next
Sleep
counting_pine
Site Admin
Posts: 6180
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Apr 03, 2006 14:50

Nice work on the indentation :)
2
Posts: 75
Joined: Mar 28, 2006 21:01

It's really not so hard 2 indent it!

Postby 2 » Apr 03, 2006 17:34

FreeBasic does it automatically if I want! So I do that now.
counting_pine
Site Admin
Posts: 6180
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Apr 04, 2006 18:22

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
axipher
Posts: 891
Joined: Dec 27, 2005 16:37
Location: Sudbury,Ontario
Contact:

Postby axipher » Apr 05, 2006 0:20

You just wrote a gerat binary tutorial, even I learnt quite a but of helpful info, good job counting_pine.
2
Posts: 75
Joined: Mar 28, 2006 21:01

Maybe I could do that.

Postby 2 » Apr 05, 2006 4:18

Maybe it is faster.I do love binary,so I should be using those tricks.
tunginobi
Posts: 655
Joined: Jan 10, 2006 0:44
Contact:

Postby tunginobi » Apr 05, 2006 13:00

SHR == integer divide by two
SHL == multiply by two

For lazy readers.
2
Posts: 75
Joined: Mar 28, 2006 21:01

Hey,thanks,it works.

Postby 2 » Apr 05, 2006 16:41

It does seem faster.It's sad that not all programming languages have
those features.I don't think QBasic did.
jofers
Posts: 1525
Joined: May 27, 2005 17:18
Contact:

Postby jofers » Apr 05, 2006 17:15

QBasic actually did, it optimized the assembly output to do shifts if you performed an integer divide by a power of 2. One of the few actual optimizations stuck in QB.
2
Posts: 75
Joined: Mar 28, 2006 21:01

Now that is good info! I never knew that.

Postby 2 » Apr 11, 2006 21:43

I liked QB,but it did have array limits.If it wasn't 4 QB I never
would have found FB.You know,the people here are nicer than those on the JustBasic forum.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 1 guest