BBC BASIC for Windows
Programming >> BBC BASIC language >> how to
http://bb4w.conforums.com/index.cgi?board=language&action=display&num=1394449112

how to
Post by quadpughbellsouth on Feb 2nd, 2014, 12:34pm

How do you the right shift operator ">>"

a few examples would help

thanks
nick smiley
Re: how to
Post by admin on Feb 2nd, 2014, 1:14pm

on Feb 2nd, 2014, 12:34pm, quadpughbellsouth wrote:
How do you the right shift operator ">>"
a few examples would help

The informal syntax is:

Code:
      result = number >> shift 

Where number is a numeric value (assumed integer) and shift is the number of bits by which you want to shift it.

So for example if you do:

Code:
      PRINT %10101010 >> 3 

the result will be %10101, or 21 in decimal.

For positive numbers the right-shift operator is equivalent to dividing by the equivalent power-of-two, so for example:

number >> 1 gives the same result as number DIV 2
number >> 2 gives the same result as number DIV 4
number >> 3 gives the same result as number DIV 8

But that equivalence doesn't necessarily apply for negative numbers.

Google will find you many web pages describing the shift operators and their uses, for example here (strictly it is talking about Java but the BBC BASIC shifts work just the same):

http://stackoverflow.com/questions/141525/absolute-beginners-guide-to-bit-shifting

Richard.