PAGUE 6 MESES
LEVE 12 MESES
GARANTIR DESCONTO

Fórum Javascript Bitmasking - Operações boolean com operadores Bitwise #612016

18/07/2020

0

Olá!
Lendo um livro acabei encontrando esse conceito de bitmasking.
The second way to use bitwise operators is a technique known as a bitmask. Bitmasking
is a popular technique in computer science when there are a number of Boolean options
that may be present at the same time. The idea is to use each bit of a single number to
indicate whether or not the option is present, effectively turning the number into an
array of Boolean flags. Each option is given a value equivalent to a power of 2 so that
the mask works. For example:
var OPTION_A = 1;
var OPTION_B = 2;
var OPTION_C = 4;
var OPTION_D = 8;
var OPTION_E = 16;
With the options defined, you can create a single number that contains multiple settings
using the bitwise OR operator:
var options = OPTION_A | OPTION_C | OPTION_D;
You can then check whether a given option is available by using the bitwise AND
operator. The operation returns 0 if the option isn’t set and 1 if the option is set:
//is option A in the list?
if (options & OPTION_A){
//do something

Por aprendizado e diversão, estou tentando implemented isso em um código meu:
Game_Player.prototype.canJump = function(x, y){
    return !this.isInVehicle() && this.canMove() && $gameMap.isValid(x, y) && $gameMap.isPassable(x, y, this.reverseDir(this._direction)) && !$gameMap.eventsXy(x, y).length; 
};

Se todas essas condições forem verdades, o jogador pode pular.

Decidi tentar implementar o bitmasking nessas condições, mas estou meio perdido em como aplicar o conceito, visto que a principio, os valores não são números.
Então fiz o seguinte:
Game_Player.prototype.canJump = function(x, y){
    const vehicle       = Number(!this.isInVehicle()); // Retorna 1 se for true e 0 se for false.
    const move          = Number(this.canMove());
    const isValid       = Number($gameMap.isValid(x, y));
    const isPassable    = Number($gameMap.isPassable(x, y, this.reverseDir(this._direction)));
    const eventsXy      = Number(!$gameMap.eventsXy(x, y).length);
    return ? 
};

Agora todas as condições são números.
Mas e então? rsrsrs to perdido. Alguém para ajudar?
Eliaquim Nascimento

Eliaquim Nascimento

Responder

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar