2^nth BS1
Hi,
I was just wondering if there is an equivalent to DCD or any easier way to find a certain power of 2 using a BS1. Right now I'm using a lookup command, but it eats up alot of valuable code space.
Since I'm using a BS1, anything that could help me save some extra code space would be a great help!![smilewinkgrin.gif](http://forums.parallax.com/images/smilies/smilewinkgrin.gif)
Thanks!
I was just wondering if there is an equivalent to DCD or any easier way to find a certain power of 2 using a BS1. Right now I'm using a lookup command, but it eats up alot of valuable code space.
LOOKUP CheckRand, (1,2,4,8,16,32,64,128,256,512), CheckRand
Since I'm using a BS1, anything that could help me save some extra code space would be a great help!
![smilewinkgrin.gif](http://forums.parallax.com/images/smilies/smilewinkgrin.gif)
Thanks!
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
FOR I = 1 to NumBits
Result = Result * 2
NEXT I
Rats!... It's been awhile since I have programmed with the BS1. I was trying to think of a clever way to use the BIT operator,
but I couldn't get anything to gel. I don't have a BS1 in front of me to test the code on so it makes things tough.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe
IC Layout Engineer
Parallax, Inc.
Post Edited (Beau Schwabe (Parallax)) : 8/14/2006 6:13:53 AM GMT
That doesn't seem to work. Looking in the help file, ^ is the Bitwise XOR operator. If only it was that easy to do...
Are there any other alternatives?
Allanlane5's suggestion should work fine... I was talking to Jeff Martin this morning about an unorthodox method using the
WRITE command, that he thought would probably work..."actually better on the BS1 than the BS2 since the BS1's tokens
were byte oriented"
Basically, you need to work within the W0 variable with the BIT command and as your result.
Through trial and error of looking at the memory map... see what the difference is in your code between....
BIT0 = 1
BIT1 = 1
BIT2 = 1
BIT3 = 1
...
...
...
BIT15 = 1
....Once you determine where the BYTE in memory is that is changing, you can design a self-modifying section of code that might
look like...
...Say for example that at memory location 40 you noticed that the value for BIT0 = 1 is $20, and the value at the same location
for BIT15 = 1 would change to $2F.
Setting 'N' (<- Could be a constant or another variable) in the above program would effectively change the next line of code to
correspond to the proper BIT setting. The "Result" would simply be what was in W0.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe
IC Layout Engineer
Parallax, Inc.
I did actually implement something like this and it worked amazingly well and also greatly simplified my code, but then I realized that there is a finite amount of times you can write to the EEPROM before it doesn't function anymore. Since my program does these calculations every second, its probably not the best way to do it.
Allanlane5's solution does work, but it takes up just as much space as the lookup function I am currently using. I was just wondering if there was any smaller/easier way to do it, but I guess I'll have to stick with what I have. If only I had a BS2, things would be so much easier...
I stand to be corrected, but I don't think Allen's proposed code covers the zeroth power of 2, which make me slightly wary:
2^0 = 1
I'm guessing that the first power will work okay, but I get no assurances from the PBASIC Help File as to the actual sequence of the limit "timing". If START and LIMIT are both 1, will it iterate once, or terminate immediately?
2^1 = 2
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
<!--StartFragment -->
Result = 1
FOR I = 1 to NumBits...
Well, if NumBits is zero, then the body of the 'FOR' loop should not execute, and you get a result of 1.
You'd have to try this on the BS2 -- I don't have one handy right now or I'd try it myself.
As far as it goes for a BS1, if you have a FOR loop such has "FOR I = 1 TO 0", the body of the loop will be executed once for some reason.