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

2
Posts: 75
Joined: Mar 28, 2006 21:01

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

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 dClsDim a(d) As Ubytea(0)=1For 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    PrintNextSleep`
counting_pine
Posts: 6180
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs
Nice work on the indentation :)
2
Posts: 75
Joined: Mar 28, 2006 21:01

### It's really not so hard 2 indent it!

FreeBasic does it automatically if I want! So I do that now.
counting_pine
Posts: 6180
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs
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, cc = a AND bprint bin(a, 8) '01010101print bin(b, 8) '00001111print 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, bb = a SHR 3print bin(a, 8) '11011010print 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 dClsDim a(d) As Ubytea(0)=1For 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    PrintNext`
axipher
Posts: 891
Joined: Dec 27, 2005 16:37
Location: Sudbury,Ontario
Contact:
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.

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:
SHR == integer divide by two
SHL == multiply by two

2
Posts: 75
Joined: Mar 28, 2006 21:01

### Hey,thanks,it works.

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:
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.

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.