Page 1 of 1

c to basic convert

Posted: Mon Aug 21, 2006 12:33 pm
by Seb McClouth
I was wondering if someone could help me out with the following:

Code: Select all

lps <<= 1
According to what I have found this means that the value of lps is bitshifted to the left (I think only 1), and the '=' is for an assignment. But that means that in the code it is used:

Code: Select all

while (lps <<= 1)
the loop is unendless for the reason that, according to my knowledge, as long as lps 'shifted to the left' is 1 then run the loop.

grtz

Posted: Tue Aug 29, 2006 3:14 pm
by DrV
lps <<= 1 is essentially equivalent to lps = lps * 2, assuming you are running outside the IDE so that overflow doesn't cause an error.

The while (lps <<= 1) loop will run until the last 1 bit "falls off" the high end of lps (which is probably a 32-bit int). Similar code for BASIC:

Code: Select all

do while lps <> 0
  lps = lps * 2
  ' loop body..
loop

Posted: Sat Sep 02, 2006 3:04 pm
by Seb McClouth
DrV wrote:lps <<= 1 is essentially equivalent to lps = lps * 2, assuming you are running outside the IDE so that overflow doesn't cause an error.

The while (lps <<= 1) loop will run until the last 1 bit "falls off" the high end of lps (which is probably a 32-bit int). Similar code for BASIC:

Code: Select all

do while lps <> 0
  lps = lps * 2
  ' loop body..
loop
What do you mean with "falls off"?

Posted: Mon Sep 04, 2006 6:35 pm
by RyanKelly
[quote="Seb McClouthWhat do you mean with "falls off"?[/quote]

The << operator is intended to perform bit shifting WITHOUT rotation. The proccessor has an assortment of bit shifting instructions, some with rotation and some without, some that transfer the high or low bit to the carry flag or copy the carry flag into the high or low bit. Few languages provide built in operators for all of this functionality. The most common is the simple shifting without rotation, so is your case is the only bit set is the high bit, the shift will cause it to "fall off" and the result will be a bit field with all the bits set to 0.

Posted: Thu Nov 30, 2006 7:09 am
by Seb McClouth
Hey

I've tried to convert this

Code: Select all

unsigned long loops_per_sec = 1;
228 
229 static void calibrate_delay(void)
230 {
231         int ticks;
232 
233         printk("Calibrating delay loop.. ");
234         while (loops_per_sec <<= 1) {
235                 ticks = jiffies;
236                 __delay(loops_per_sec);
237                 ticks = jiffies - ticks;
238                 if (ticks >= HZ) {
239                         __asm__("mull %1 ; divl %2"
240                                 :"=a" (loops_per_sec)
241                                 :"d" (HZ),
242                                  "r" (ticks),
243                                  "" (loops_per_sec)
244                                 :"dx");
245                         printk("ok - %lu.%02lu BogoMips\n",
246                                 loops_per_sec/500000,
247                                 (loops_per_sec/5000) % 100);
248                         return;
249                 }
250         }
251         printk("failed\n");
252 }
For as far possible ofcourse since I don't have the ___asm___ thingy. I wrote the following:

Code: Select all

SUB CalibrateDelay
Do While LoopsPerSec! <> 0 'needs to be! for use with Delay
 LoopsPerSec! = LoopsPerSec! * 2
 ticks = jiffies
 Delay LoopsPerSec!
 ticks = jiffies - ticks
 If LoopsPerSec! >= HZ THEN 'HZ = 100
  Print "Passed."
  EXIT LOOP
 ELSE
  Print "Failed."
  EXIT LOOP
 END IF
LOOP
END SUB

'The Delay Sub I'm using:
SUB Delay (loops!)
 T! = Timer + loops!
 PT! = Timer
 Do: Loop Until Timer >= T! Or Timer < PT!
END SUB
Fairly easy said... it does nothing. Can someone enlight me/help me out?