c to basic convert

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
User avatar
Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:

c to basic convert

Post by Seb McClouth » Mon Aug 21, 2006 12:33 pm

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
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.

DrV
Veteran
Posts: 63
Joined: Thu Jun 02, 2005 9:44 pm

Post by DrV » Tue Aug 29, 2006 3:14 pm

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

User avatar
Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:

Post by Seb McClouth » Sat Sep 02, 2006 3:04 pm

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"?
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.

RyanKelly
Coder
Posts: 48
Joined: Sun Jan 22, 2006 6:40 pm
Contact:

Post by RyanKelly » Mon Sep 04, 2006 6:35 pm

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

User avatar
Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:

Post by Seb McClouth » Thu Nov 30, 2006 7:09 am

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?
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.

Post Reply