'=========================================================================== ' Subject: ASSEMBLY BIT SHIFTING ROUTINES Date: 12-19-97 (07:33) ' Author: Beau Schwabe Code: QB, QBasic, PDS ' Origin: comp.lang.basic.misc Packet: BINARY.ABC '=========================================================================== '>Hello. One of the problems I have repeatedly with basic is that '>it has no shift operation, i.e. the fast multiply by a power of '>two. I find that on average any hideously fast code I write '>has to use shifts a whole lot. How can one code up a shift? '>Is assembly required? ' '>Please avoid the "just multiply by 2^k" idea; unless this gets '>compiled into a shift, it takes about 100x longer on modern processors, '>and that's taking into account the FPU. 'Ok... I agree that since QBASIC or QuickBasic4.5 does NOT allow for 'these conversions as a BUILT-IN function, you must write your own 'code which does take more time, to do the job. ...But on the other 'hand if you were to write an Assembly program to be run from BASIC, 'that the TIME required to call the assembly program,pass the arguments, 'do the conversion, and pass the result back to basic can and is a 'lengthy process as well. I have provided only a solution using the 'built-in functions of basic in a previous post... here is another 'solution using a hybrid Assembly-Basic that will also work, however 'I don't claim a dramatic if any speed-increase because of the reasons 'I mention above. 'Example program STARTS here.......................................... 'Example Hybrid Assembly-Basic program to do low level BIT manipulation 'on BYTE values. 'If you are using QuickBasic4.5, then you must load it with the /L 'option. 'If you are using QBASIC, then the program is ready to run. Assembly$ = "558BEC505351B4008B5E068A0F8B5E088A07D2C0890759" Assembly$ = Assembly$ + "5B588BE55DCA0400CC558BEC505351B4008B5E068A0F8B" Assembly$ = Assembly$ + "5E088A07D2C88907595B588BE55DCA0400CC558BEC5053" Assembly$ = Assembly$ + "51B4008B5E068A0F8B5E088A07D2E08907595B588BE55D" Assembly$ = Assembly$ + "CA0400CC558BEC505351B4008B5E068A0F8B5E088A07D2" Assembly$ = Assembly$ + "E88907595B588BE55DCA0400CC" Code$ = "" WHILE LEN(Assembly$) Code$ = Code$ + CHR$(VAL("&H" + MID$(Assembly$, 1, 2))) Assembly$ = MID$(Assembly$, 3) WEND ROL% = SADD(Code$) ROR% = ROL% + 32 SHL% = ROR% + 32 SHR% = SHL% + 32 'Value% = 8-BIT value 'N% = Number of BITS to shift 'CALL ABSOLUTE(Value%, N%, ROL%) 'Rotate Value% Left N% Bits 'CALL ABSOLUTE(Value%, N%, ROR%) 'Rotate Value% Right N% Bits 'CALL ABSOLUTE(Value%, N%, SHL%) 'Shift Value% Left N% Bits 'CALL ABSOLUTE(Value%, N%, SHR%) 'Shift Value% Right N% Bits 'Note: ' Value% changes accordingly. Value% = 128 N% = 2 CALL ABSOLUTE(Value%, N%, SHR%) 'Shift Value% Right N% Bits PRINT Value%