![]() |
Forum Index : Microcontroller and PC projects : Shift Left or Right in MMBasic
Page 1 of 2 ![]() ![]() |
|||||
Author | Message | ||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3272 |
I am working on adding some features to the Micromite and I was considering the facility to shift the bits in a number to the left or right. To my mind there are two different syntaxes that one could adopt. The first is a function. SHIFT(nbr, bits) where 'nbr' is the number to be shifted and 'bits' is the number of bits to shift left if 'bits' is positive or shift right if 'bits' is negative. For example: A = SHIFT(&B010000, -2)
would result in A containing &B000100 The other is to use the << and >> operators as used in the C language. This takes the form of nbr << bits for shift left or nbr >> bits for shift right. For example: A = &B010000 >> 2
would result in A containing &B000100 As a more complex example, to shift a number to the left by eight bits (one byte) and then set the lower bits of the result to &B1010: A = SHIFT(A, 8) OR &B1010
or A = A << 8 OR &B1010
To my mind the first is more understandable for newcomers to programming (which is a lot of what the Micromite is about) but is clumsy while the second is more convenient but also more obtuse. I am leaning towards the first but I wondered what the collective wisdom of the forum thought? Geoff Geoff Graham - http://geoffg.net |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2927 |
Geoff, From a 'beginners' perspective I would definately suggest the first option. Basic is meant to be 'simple' so the use of the word SHIFT is pretty self explainatory. << and >> are also arguably self explainatory to experienced coders but a beginner will think of 'less than' and 'greater than' However, in your first example you use - for right and + for left. Personally I would reverse these so - is left (1's move backwards) and + is right (1's move forward). Better still, why not use L and R prefixes to the numberr? WW |
||||
Grogster![]() Admin Group ![]() Joined: 31/12/2012 Location: New ZealandPosts: 9502 |
My 2c is the << or >> option. It is true that newcomers might like the SHIFT word though, for ease of reading. However, once you know that << or >> means shift left or shift right, is it not the same thing as knowing what the SHIFT word means? I guess in the true nature of BASIC, word type commands are more familiar. Smoke makes things work. When the smoke gets out, it stops! |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2927 |
Maybe csider calling the command BITSHIFT or BSHIFT (meaning 'binary shift'-or- 'bit shift') Beginners could expect SHIFT(1234,1) to become 234 (keeping + ias left shift) if it isn't made clear it is a bit / binary shift |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2927 |
Since when did you prefer the C syntax ![]() |
||||
Grogster![]() Admin Group ![]() Joined: 31/12/2012 Location: New ZealandPosts: 9502 |
I just KNEW someone would pick up on that! ![]() To me, it's just simpler, once you know what it means. ...not that I am claiming that C is simple, once you know what it means... ![]() I'm glad you remembered to put the F in BSHIFT... Smoke makes things work. When the smoke gets out, it stops! |
||||
G8JCF![]() Guru ![]() Joined: 15/05/2014 Location: United KingdomPosts: 676 |
Hi Geoff I would prefer << and >>, if not then LShift and RShift. The "-" minus parameter is really odd (although mathematically logical). Personally I'm nae convinced of the need for << and >>, since *2 and \2 do the same thing. If it's easy to implement and doesn't prevent other functions such as TRIM/RTRIM/LTRIM and/or +=, ++, --, being added then please add << >> LShift RShift by all means. So in summary Var << N Shift Left Var N Binary places to the left equiv to Var = Var * 2 Var >> N Shift Right Var N Binary places to the Right equiv to Var = Var \ 2 OR LShift(Var,N) Shift Left Var N Binary places to the left equiv to Var = Var * 2 RShift(Var,N) Shift Right Var N Binary places to the Right equiv to Var = Var \ 2 BTW, out of interest, BASIC is essentially a base 10 language, so the 'SHIFT' operations should really mean \10 and *10. Why are we so convinced SHIFT means \2 and *2 ? 73 Peter The only Konstant is Change |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2927 |
![]() ![]() ![]() ![]() |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2927 |
Trouble with LSHIFT and RSHIFT is that I beleive Geoff would treat these as 2 extra command indexes in a limited remaining 'pot'. So better to have one command with parameter to define left or right |
||||
Oldbitcollector![]() Senior Member ![]() Joined: 16/05/2014 Location: United StatesPosts: 172 |
+1 on the << >> usage! That will be a very handy command. My Propeller/Micromite mini-computer project. |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2927 |
it would be extremely useful if after the Shift command has executed, that there be a way of reading what was shifted 'out'. This way you can do one bit shift, read the 'out' variable and set a pin (i.e. useful for bit-banging) I know this can be done in many other ways, but adding the ability to read an 'out' value gives another method. Possibly read MM.SHIFT value? Useful for hex calculations when shifting by four bits. |
||||
G8JCF![]() Guru ![]() Joined: 15/05/2014 Location: United KingdomPosts: 676 |
@WW What you are asking for is MM.Carry ! Yes indeed, but if someone has difficulty grasping << >> then carry must surely be a step beyond !! re LShift, Rshift something like SHIFT R|L, Var, Places eg A=&HFF SHIFT R,A,1:Print Hex$(A): &H7F SHIFT L,A,1:Print Hex$(A): &HFE OK, but nae as tidy as << >> LSHF RSHF Peter The only Konstant is Change |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2927 |
Hi Peter, I am not saying >> or << is wrong, Im just putting myself in the shoes of a beginner learning basic. With > and < already known as greater/less than signs, then the command SHIFT is much more 'fitting' to the Basic simplicity/meaning. As soon as C syntax be introduced to MMBasic then I believe it will become like all the other versions that are more like C than Basic. Should Geoff add ++ and -- too? ( C programmers will certainly say yes!) So nothing wrong with << or >>, just SHIFT 'fits' better with all the other commands especially for the beginners. Just my C worth as Grogs would say! (sorry 2c's worth - Im getting carried away with C ![]() |
||||
robert.rozee Guru ![]() Joined: 31/12/2012 Location: New ZealandPosts: 2405 |
whereas -- and ++ are an abomination unto nuggan, seeing as they cause that unholy thing called a "side effect", << and >> as is being suggested are just normal binary operators. pascal has a rather useful function called ODD(x), which returns true if x is odd, and false if x is even. if, at the same time, you were introducing 32-bit integers, then "odd(x)" and "x<0" would allow you to check the states of the LSB and MSB respectively as is being requested by others. does << and >> save on using up a scarce function tag? if it does, then i would consider that in itself enough to use this syntax. i don't see that having << and >> makes any concession to use other (more evil) C operators. rob :-) |
||||
BobD![]() Guru ![]() Joined: 07/12/2011 Location: AustraliaPosts: 935 |
My vote is << and >> and if people are experienced enough to want something like this function then they should be able to handle the syntax. |
||||
twofingers Guru ![]() Joined: 02/06/2014 Location: GermanyPosts: 1535 |
I'm not yet a µMite user but I would prefer >> and << (and hope it will some day emerge to Maximites). It's also VB syntax. On the other hand: Perhaps Shift() is more economical because it takes only one token? Btw. do we have a reliable string library already (InStr(), Trim(), Ucase$, Lcase$, Like ...)? Just 2c more. Michael causality ≠correlation ≠coincidence |
||||
MicroBlocks![]() Guru ![]() Joined: 12/05/2012 Location: ThailandPosts: 2209 |
<< and >> has my vote. It is an operator and not really a function. I also think it will execute much faster as parsing another operator is simpler then interpreting and calling a function. I am not sure if making everything simpler is a good thing. Once a beginner programmer is past the if/then do/loop for/next stage then a little more difficult will actually teach them something. The whole world is already much simpler and what did that get us? Shift would be nice to use for shifting in or out bits. But then again that can be easily done with OR/AND. Only reason to include it would be speed. (which is in my opinion a very good reason. :) ) The ++ and -- should be completely eradicated from all languages, so please don't consider adding them. BTW When shifting bits, how does that cooperate with the floating point variables? Or are 32 bit integers being worked on? :) @Robert Checking for odd is easy, you just check the last bit with an AND 1. Microblocks. Build with logic. |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6224 |
My preference would be for << and >> Most bit shifting is done on 8 or 16 bit values so floating point is not a problem. I consider them to be closely related to AND OR XOR which are already described in the MMBasic manual as bitwise operators. Checking for overflow needs the number of bits declared so it might be easier to leave that to the programmer. Jim. VK7JH MMedit |
||||
Grogster![]() Admin Group ![]() Joined: 31/12/2012 Location: New ZealandPosts: 9502 |
I agree. Manipulation of bits in a byte, is advanced knowledge, surely, so, therefore, advanced syntax rules COULD be applied - again - only my 2c. Smoke makes things work. When the smoke gets out, it stops! |
||||
G8JCF![]() Guru ![]() Joined: 15/05/2014 Location: United KingdomPosts: 676 |
Hi Geoff Since << >> SHIFT can be easily implemented using existing MMBasic operators, ie * \, I think much more important is the ability to pass arrays into SUBs and FUNCTIONs. Being able to pass arrays would lead to much better code structure further minimising the need for global variables. The only benefit of << >> SHIFT etc is for performance compared to *2...N and \2...N where N is a power of 2 as far as I can tell cf. \10...T and *10...T where T is a power of 10. @ingeneral "evil", "abomination" ![]() ![]() ![]() @RR As for ++ --, perhaps in high level environments, eg text, business processing the increment and decrement operators would be less needed, in low-level environments which are much closer to the bare metal. one is forever incrementing/decrementing counters and pointers. Additionally, although many(most) RISC processors do not implement post/pre-increment/decrement as part of their instruction set, every other type of processor does and in particular DSP processors. The uMite falls (very luckily/by Geoff's foresight) into that gap between bare metal and high-level environment (it doesn't have the space/capacity for a file-system so it can't really be used for business processing), so sometimes low-level functionality, such as << >> ++ -- AddressOf would be really useful and one has to assume that those folks who are poking about (literally) in the guts of a system would/should understand what these types of low-level functions mean and do. BTW, I believe in horses for courses, so IMHO C is really the best tool for OS and low-level systems programming, but the worst thing for business processing. I really hope we don't end up re-living the fruitless C vs Pascal vs Basic vs Algol vs Cobol vs Smalltalk vs Modula 2 vs APL vs ADA vs Fortran vs ..... "discusions" that have all long been and gone. 73 Peter 73 Peter The only Konstant is Change |
||||
Page 1 of 2 ![]() ![]() |
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |