Can someone explain Shl and Shr please

BlitzMax Forums/BlitzMax Beginners Area/Can someone explain Shl and Shr please

Chroma(Posted 2008) [#1]
shift left and shift right. What the bleep are they for?


Azathoth(Posted 2008) [#2]
Shifting bits, they can also be used as a quick way to divide or multiple the number.


Brucey(Posted 2008) [#3]
a quick way to divide or multiple the number

...by powers of 2.
eg.
Print 1 Shl 1
Print 1 Shl 2
Print 1 Shl 3
Print 1 Shl 4
Print 1 Shl 5
Print 1 Shl 6
Print 1 Shl 7
Print 1 Shl 8



xlsior(Posted 2008) [#4]
Another use is for bit-level comparisons or extractions, such as for example seperate color values into R, G and B.

col=15676216
print bin(col)
print (col & $FF0000) Shr 16  ' RED
print (col & $00FF00) Shr 8    ' GREEN
print (col & $0000FF)             ' BLUE


will print:
00000000111011110011001100111000
239 (red)
51 (green)
56 (blue)

Here the "& FF0000" and such will narrow down the string to just the portions o fthe string we want, and the SHR command will scroll the output to the right side of the string. shr 16 is the same as dividing by 65536, but:
- shr 16 can be an easier visual aid if you're familiar with binary
- shr and shl are faster than dividing or multiplying, since there is no general-purpose math routines involved. If you know you'll be dividing or multiplying by powers of two, shr and shl are a nice optimization.


Beaker(Posted 2008) [#5]
They can also be used for packing small integers together (<256, <128, <64 etc) for sending as network traffic, and unpacking again.

You can even pack small (innacurate) fixed point float values this way.


SoggyP(Posted 2008) [#6]
Hello.

They can also be used in conjunction with the 'UP' key to silence all comments or to drink from a bottle.

'SHR-UP'
'SHL-UP'

Ok, not really.

Goodbye.


ImaginaryHuman(Posted 2008) [#7]
Note also that if you shift left, bits in the upper part of the value are shifted `out` and lost, and if you shift right, lower bits are shifted out and lost. It's not the same as `rotating` the bits. If you shift left, it adds extra 0's at the lower end, and if you shift right it adds 0's at the upper end.

Shifts are borrowed from assembly language where you have commands like:

SHL (shift left)
SHR (shift right)
ROL (rotate left)
ROR (rotate right)
ASL (arithmetic shift left)
ASR (arithmetic shift right - maintains the sign of the number in the upper bit)

Vaguely, Shl and Shr are remnants of BlitzBasic's in-line assembler days. I wish the others were still available, too.

I think there are some common instructions among assembly languages on different CPU's, such that BRL could put together a small subset of cross-platform assembler instructions, like those above. Then we could at least do in-line assembly on *some* instructions.


Blueapples(Posted 2008) [#8]
First some binary. We usually count with base 10, that is we have 10 digits to make numbers with, so we go in order 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, then we start adding them together to go beyond 10 numbers: 10, 11, 12, 13, etc.

I hope everyone can count. ;-)

In binary though, we only have 2 digits to use: 0 and 1. So the same numbers go like this: 1, 10, 11, 100, 101, 110, etc.

Read more on that here: http://en.wikipedia.org/wiki/Binary_numeral_system

decimal = binary
1 = 00000001
2 = 00000010
4 = 00000100


Okay?

So 1 Shl 1 means shift all the bits in the provided value (in this case 1), 1 spot the left, and chop of the extra zero. So 1 Shl 1 = 2 because when you move those bits to the left you get the same binary number for 2.

1 Shl 1 =   0|00000010 (decimal 2)
2 Shl 1 =  00|00000100 (decimal 4)