BITLSHIFT


Shifts a number to the left by a specified amount of bits and returns the result found after the shift.

Syntax:

BITLSHIFT(number, shift_amount)

number should be 0 or higher.

shift_amount is an integer.


Example:

If number contains 1 and shift_amount contains 2:

BITLSHIFT(1, 2)

returns 4


numberOne:


numberTwo:


Result:

4

Application:

Fast Scoring in a Game


Imagine you are developing a simple arcade game where a player's score can increase based on different actions. Let's say a player gets points for collecting items. To make the scoring dynamic and fast, you decide to use a bit shift operation.


  • Normal Points: A player gets 1 point for a common item.
  • Bonus Multiplier: A "power-up" item doubles the points for subsequent items. You can represent this with a multiplier.


Instead of doing a slow multiplication (score * 2), you can use a bitwise left shift to quickly double the score.


Here is a table demonstrating how this works:

Action

Current Score (Decimal)

Current Score (Binary)

Power-Up Multiplier

BITLSHIFT Operation

New Score (Decimal)

New Score (Binary)

A
B
C
D
E
F
G
1
Start
0
0000
0
-
0
0000
2
Collect Item 1
1
0001
0
-
1
0001
3
Collect Item 2
2
0010
0
-
2
0010
4
Collect Power-Up
2
0010
1
-
2
0010
5
Collect Item 3 (with Power-Up)
2
0010
1
BITLSHIFT(2, 1)
4
0100
6
Collect Item 4 (with Power-Up)
4
0100
1
BITLSHIFT(4, 1)
8
1000
7
Collect Item 5 (Power-Up fades)
8
1000
0
-
9
1001

Explanation:


  • Initially, the score is 0.
  • After collecting the first two items, the score is 2. The binary representation is 0010.
  • The player then collects a "Power-Up" item. The multiplier is now 1.
  • When the player collects "Item 3," the BITLSHIFT function is applied to the current score (2) by a shift of 1.
    • Decimal: BITLSHIFT(2, 1)
    • Binary: The bits of 0010 are shifted one position to the left, resulting in 0100.
    • 0100 in binary is equal to 4 in decimal. The score is instantly doubled.
  • When "Item 4" is collected, the function is applied to the new score (4) with a shift of 1.
    • Decimal: BITLSHIFT(4, 1)
    • Binary: The bits of 0100 are shifted one position to the left, resulting in 1000.
    • 1000 in binary is equal to 8 in decimal. The score is doubled again.


This example illustrates how BITLSHIFT can be a very efficient way to multiply by powers of two, which is a common requirement in many programming tasks where performance is critical.





This page is protected by Google reCAPTCHA. Privacy - Terms.
 
Built using Zapof