Shop OBEX P1 Docs P2 Docs Learn Events
2^nth BS1 — Parallax Forums

2^nth BS1

akudaikonakudaikon Posts: 10
edited 2006-08-15 22:29 in BASIC Stamp
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.

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

Thanks!

Comments

  • FranklinFranklin Posts: 4,747
    edited 2006-08-13 19:42
    Shift right will multiply by 2

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • akudaikonakudaikon Posts: 10
    edited 2006-08-13 20:45
    Unfortunately, shift right isn't available on BS1's.
  • allanlane5allanlane5 Posts: 3,815
    edited 2006-08-13 21:12
    Result = 1
    FOR I = 1 to NumBits
    Result = Result * 2
    NEXT I
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2006-08-13 22:02
    CheckRand = 2^CheckRand

    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
  • akudaikonakudaikon Posts: 10
    edited 2006-08-14 05:13
    Beau Schwabe (Parallax) said...
    CheckRand = 2^CheckRand

    That doesn't seem to work. Looking in the help file, ^ is the Bitwise XOR operator. If only it was that easy to do... sad.gif

    Are there any other alternatives?
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2006-08-14 11:32
    How about allanlane5's suggestion?
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2006-08-14 17:31
    PJ Allen,

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

    WRITE   40,$20+N          'WRITE Location{0-255}, DataItem{0-255}
    BIT0 = 1                      
    
    



    ...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.
  • akudaikonakudaikon Posts: 10
    edited 2006-08-14 20:41
    Beau Schwabe (Parallax) said...
    PJ Allen,

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

    WRITE   40,$20+N          'WRITE Location{0-255}, DataItem{0-255}
    BIT0 = 1                      
    
    



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

    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... tongue.gif
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-08-14 22:24
    Gents -

    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 -->
  • allanlane5allanlane5 Posts: 3,815
    edited 2006-08-15 17:14
    Well, it should cover that case.

    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.
  • akudaikonakudaikon Posts: 10
    edited 2006-08-15 22:14
    allanlane5 said...
    Well, it should cover that case.

    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.
  • allanlane5allanlane5 Posts: 3,815
    edited 2006-08-15 22:29
    Oh, darn, so in the body of the For loop, you need a "if NumBits = 0 THEN Skipit"
Sign In or Register to comment.