Outerra forum

Please login or register.

Login with username, password and session length
Advanced search  

News:

Outerra Tech Demo download. Help with graphics driver issues

Author Topic: bit mask  (Read 3169 times)

theshanergy

  • Member
  • **
  • Posts: 69
bit mask
« on: February 13, 2015, 11:24:51 pm »

We have lights! Awesome! Now can somebody explain how to use a 'bit mask'? I've been Googling this but am super confused. I've never come across bitwise operators before and the documentation I've found is not computing in my brain. Is there some simple equation I can use to select n rows of spotlights after my mask variable is declared?
Logged

cameni

  • Brano Kemen
  • Outerra Administrator
  • Hero Member
  • *****
  • Posts: 6721
  • No sense of urgency.
    • outerra.com
Re: bit mask
« Reply #1 on: February 14, 2015, 01:56:35 am »

A bit mask is basically a binary number, in this case used so that 1's select the corresponding light for given operation. Rightmost bit (bit 0) corresponds to the first light, which also has id 0.

In newer Windows versions there's an updated calculator that you can switch to programmer mode (alt+3), and toggle between basic number bases. Enter a binary number and switch it back to decimal, and you get the mask in decimal.
Logged

theshanergy

  • Member
  • **
  • Posts: 69
Re: bit mask
« Reply #2 on: February 16, 2015, 10:21:07 pm »

I'm still trying to wrap my head around this..

Given the following code:
Code: [Select]
//turn signals
  var turn =
  this.add_spotlight_source({x:-0.98,y:-4.3,z:1.06}, {y:-1}, 0.02, 54, 0.8, {x:0.1,y:0.08}, 0);
  this.add_spotlight_source({x:0.98,y:-4.3,z:1.06}, {y:-1}, 0.02, 54, 0.8, {x:0.1,y:0.08}, 0);
  this.add_spotlight_source({x:-1.12,y:-4.28,z:1.03}, {y:-1}, 0.02, 54, 0.8, {x:0.1,y:0.06}, 0.02);
  this.add_spotlight_source({x:1.12,y:-4.28,z:1.03}, {y:-1}, 0.02, 54, 0.8, {x:0.1,y:0.06}, 0.02);
  this.add_spotlight_source({x:-0.69,y:4.55,z:1.39}, {y:1}, 0.2, 54, 0.8, {x:0.2,y:0.06}, 0.02);
  this.add_spotlight_source({x:0.69,y:4.55,z:1.39}, {y:1}, 0.2, 54, 0.8, {x:0.2,y:0.06}, 0.02);
  this.add_spotlight_source({x:-1.23,y:4.3,z:1.74}, {x:-1}, 0.1, 94, 0.8, {x:0.1,y:0.03}, 0.02);
  this.add_spotlight_source({x:1.23,y:4.3,z:1.74}, {x:1}, 0.1, 94, 0.8, {x:0.1,y:0.03}, 0.02);
  turnlmask = 0x55<<turn;
  turnrmask = 0xaa<<turn;

If I convert 0x55 to binary I get '0000 0000 0101 0101'. Is this to say that the first light in the list is on, the second off, the third on, and so forth until the 7th row?

So for example, if I wanted to define new lights:
Code: [Select]
var example_lights =
this.add_spotlight_source(params);
this.add_spotlight_source(params);
this.add_spotlight_source(params);

example_mask = 7<<example_lights;

Would that turn them all on? What exactly is the '<<' doing? Also, how does the var example_lights contain all three rows of lights in this example? I always thought a semicolon ended a variable declaration?

Sorry if these are noobish questions - I'm a front end web guy and I've never encountered this syntax or usage before.
Logged

cameni

  • Brano Kemen
  • Outerra Administrator
  • Hero Member
  • *****
  • Posts: 6721
  • No sense of urgency.
    • outerra.com
Re: bit mask
« Reply #3 on: February 17, 2015, 02:49:56 am »

If I convert 0x55 to binary I get '0000 0000 0101 0101'. Is this to say that the first light in the list is on, the second off, the third on, and so forth until the 7th row?

Right. Though technically it only defines a bit mask that selects the first, third fifth and seventh light for some later operation.

Quote
So for example, if I wanted to define new lights:
Code: [Select]
var example_lights =
this.add_spotlight_source(params);
this.add_spotlight_source(params);
this.add_spotlight_source(params);

example_mask = 7<<example_lights;

Would that turn them all on? What exactly is the '<<' doing? Also, how does the var example_lights contain all three rows of lights in this example? I always thought a semicolon ended a variable declaration?

<< is a left bit shift, so 7 << example_lights shifts number 7 (which is 111 in binary) by example_lights bits to the left, so there will be example_lights number of zeros after them.

The example_lights variable above contains just the id of the first of the defined spotlights, the semicolon indeed ends the declaration. Bit mask of that light would be (1<<example_lights), since that shifts one set bit to the correct position in the bit mask. But since the returned ids are sequential, (7<<example_lights) will create a bit mask with 3 subsequent bits set to ones, starting from example_lights position.
Logged