Problem 1 << 2

BlitzMax Forums/BlitzMax Beginners Area/Problem 1 << 2

MacSven(Posted 2008) [#1]
I have a c program that a line called like this:

1 << 2

What does this mean.
How can i use this in Blitzmax?


sswift(Posted 2008) [#2]
I think that means bitwise shift left by 2. I don't remember the max command for doing that, but unless I'm getting my bit shifts backwards, it's just a faster way of multiplying by 4.

Bitwise shifting results in a multiply or a divide when used on integers. If N is the number of places shifted, then the multiply or divide will be 2^N, so a shift of 3 bits would be * or / 8, 4 would be * or / 16, etc. Unless you know exactly what you're doing, don't attempt bitwise shifts on floats.


Brucey(Posted 2008) [#3]
1 Shl 2


:o)


GfK(Posted 2008) [#4]
Unless you know exactly what you're doing, don't attempt bitwise shifts on floats.
Why not? Will it start World War 3?

Attempting stuff when you don't know what you're doing is called experimenting, and is often the best way to learn.


MacSven(Posted 2008) [#5]
OK. Thanx i have it.
Works in my BlitzMax program!


sswift(Posted 2008) [#6]
Gfk:
And tell me, what will you learn if you attempt a bitwise shift on a float, and get garbage numbers that don't appear to have any rhyme or reason to them?

You will learn not to do that.

I just saved him time by telling him what the results of said experiment would be, and prevented him from having a confusing bug in his code which could take hours to track down because someone told him bitwise shifts multiplied by 4 but neglected to mention that it doesn't work on floats.

I myself don't know exactly what the result would be if I shifted a float, but I do know the format of floats is special and shifting them will likely do something zany like shift bits of your number into the exponent. And that's a completely useless operation for anything I can think of. So I just don't think it's something worthwhile to spend a lot of time learning. "Don't do it." is enough information for most people.

If someone asked you what happens when you mix ammonia and bleach, would you tell them it was dangerous and that they shouldn't do it, or would you let them experiment? Same thing. I saved him from a potentially life theatening three hours of bug hunting. :-)


CS_TBL(Posted 2008) [#7]
In BMax bitwise operators can only be used with integers, so floats aren't part of any discussion here.. :P


sswift(Posted 2008) [#8]
That's never stopped us before!