Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bit Shifts
#1
C provides bit shift functions >> and << which aren't available in Sinclair or ZX basic. Since I happened to have a need for such a function, I decided to drop them up here. Again, this is something that's easy on the CPU.

They work with Long Unsigned integers - and will work fine with smaller values, or you can use them as a template for 16 or 8 bit versions in assembler.

Code:
FUNCTION bitL(num as uLong) as uLong
  REM we get DEHL as uLong
  asm
   SLA  L
   RL  H
   RL  E
   RL  D
  END asm
END FUNCTION

Code:
FUNCTION bitR(num as uLong) as uLong
  REM we get DEHL as uLong
  asm
   SRA  D
   RES 7,D ; makes a zero shift into D instead of what was there originally. If you want this to work with signed numbers, you could always change it to res 6,d.
   RR  E
   RR  H
   RR  L
  END asm
END FUNCTION
Reply
#2
Not correct! ZXBasic Compiler offers Bit Shifting:
SHL and SHR for shifting left or right :wink:
------------------------------------------------------------
http://lcd-one.da.ru redirector is dead
Visit my http://members.inode.at/838331/index.html home page!
Reply
#3
I didn't know that was in there.

Huh.

I can't be responsible for not knowing keywords that aren't listed in the lexicon http://www.boriel.com/wiki/en/index.php/...dentifiers I think!

Good to see you're still around. How are you feeling?
Reply
#4
Then again:

Test.bas:46: Syntax Error. Unexpected token 'SHL' <SHL>
Reply
#5
Thanks, Its better from day to day...

<!-- l --><a class="postlink-local" href="http://www.boriel.com/forum/viewtopic.php?f=13&t=302">viewtopic.php?f=13&t=302</a><!-- l -->
Version 1.0.8: "Added shift bit instructions (SHL, SHR)"
This function is not in the lexicon yet, like some other. If you read all announcements of new versions, you will find some other commands that are not in the lexicon yet too.
I dont know how to add yet, but there are some problems in the lexicon: "Identifiers shown in bold are taken from the Sinclair BASIC"
But ITALIC and BOLD weren't in the lexicon before, and they are there now, but in bold! In the Sinclair BASIC syntax there are no ITALIC and BOLD.
@boriel, can you please correct it?
Edit:
@britlion
try this:
Code:
dim a as ubyte
dim b as ubyte
a=34
b=a shl 1
print b
Strange, but it does not work correctly with Uintegers, only Ubyte and ULong. Using UInteger gives "Traceback (most recent call last)" error. Another Bug?
------------------------------------------------------------
http://lcd-one.da.ru redirector is dead
Visit my http://members.inode.at/838331/index.html home page!
Reply
#6
Aha. That's the syntax.

I unbolded the bold and italic entries for you. You can change them as well, you know :-)

It's almost certainly my fault they they are in bold in the first place. I believe I put them into the list.

I added SHL and SHR to the list as well. They need a documentation page though. I think one for both of them would be fine.
Reply
#7
britlion Wrote:Aha. That's the syntax.
I unbolded the bold and italic entries for you. You can change them as well, you know :-)
Not with this forums account... I think, I shall create a new account to edit the lexicon.
This is the text from my Quickhelp in EXIDE for SHL/SHR function:
"value SHL times - returns the left side value binary shifted left by the number of times defined right (<<)"
"value SHR times - returns the left side value binary shifted right by the number of times defined right (>>)"
britlion Wrote:It's almost certainly my fault they they are in bold in the first place. I believe I put them into the list.
No problem, anyway, the Fail of Uinteger Shifting can be put in the 1.2.5 bug list
britlion Wrote:I added SHL and SHR to the list as well. They need a documentation page though. I think one for both of them would be fine.
Not found it yet... :-)
------------------------------------------------------------
http://lcd-one.da.ru redirector is dead
Visit my http://members.inode.at/838331/index.html home page!
Reply
#8
That's really weird. I did change it. I'm sure.

Maybe I hit "preview" and then never hit save.

I've re-edited them in, now.
Reply
#9
There were bugs with Uinteger and Integer using SHR and SHL (possible stack corruption and wrong values). Also a compiler crash if -O3 was used. This has been fixed. I'm uploading new version 1.2.5r1489c: <!-- m --><a class="postlink" href="http://www.boriel.com/files/zxb/zxbasic-1.2.5r1849c.msi">http://www.boriel.com/files/zxb/zxbasic-1.2.5r1849c.msi</a><!-- m -->
Reply
#10
Woohoo!
Reply
#11
boriel Wrote:There were bugs with Uinteger and Integer using SHR and SHL (possible stack corruption and wrong values). Also a compiler crash if -O3 was used. This has been fixed. I'm uploading new version 1.2.5r1489c: <!-- m --><a class="postlink" href="http://www.boriel.com/files/zxb/zxbasic-1.2.5r1849c.msi">http://www.boriel.com/files/zxb/zxbasic-1.2.5r1849c.msi</a><!-- m -->

That was Fast! Thank you!!!
------------------------------------------------------------
http://lcd-one.da.ru redirector is dead
Visit my http://members.inode.at/838331/index.html home page!
Reply
#12
LCD Wrote:
boriel Wrote:There were bugs with Uinteger and Integer using SHR and SHL (possible stack corruption and wrong values). Also a compiler crash if -O3 was used. This has been fixed. I'm uploading new version 1.2.5r1489c: <!-- m --><a class="postlink" href="http://www.boriel.com/files/zxb/zxbasic-1.2.5r1849c.msi">http://www.boriel.com/files/zxb/zxbasic-1.2.5r1849c.msi</a><!-- m -->

That was Fast! Thank you!!!

Ok, latest version is 1.2.5r1489d: <!-- m --><a class="postlink" href="http://www.boriel.com/files/zxb/zxbasic-1.2.5r1489d.msi">http://www.boriel.com/files/zxb/zxbasic-1.2.5r1489d.msi</a><!-- m --> hich (I believe), fixes all found bugs.

I think we must close the current buglist, and release the 1.2.5 final (stable), and start on 1.2.6. This time I will implement bitwise operators:

| / bOR
& / bAND
~ / bXOR
! / bNOT

and Logical XOR (explained on other thread). As these are new features, yes, there might be the chance for new bugs to appear... :?
So first, let's close 1.2.5 and release it, so we always have an stable version we can go back when necessary (e.g. when comparing compilations or execution behaviors).
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)