Help on Understanding this bit of code

● ARCHIVED · READ-ONLY
Started by Milena 3 posts View original ↗
  1. I have been dabbling with someone's script and I seem not to be able to understand what this bit of codes mean:

    def tile_flash(x, y, red, green, blue) if $game_map.valid?(x, y) && !@spriteset.nil? colour = ((red.to_i & 0xF0) << 4) | (green.to_i & 0xF0) | ((blue.to_i & 0xF0) >> 4) @spriteset.set_flash_tile(x, y, colour) end endCan someone explain this exact part?

    colour = ((red.to_i & 0xF0) << 4) | (green.to_i & 0xF0) | ((blue.to_i & 0xF0) >> 4)Thank you very much.
  2. that's a sequence of bit-operations, most probably to combine the numbers for the three color parts into a single bit-sequence of all color data.


    I don't have time to check and explain everything, but if you search the internet for tutorials on bit operations, you should be able to read this form of mathematics.


    Basically you need to think of red, green and blue no longer as integers, but as bit-numbers. Then the AND-operator (&) strips all those sequences down to the higher four bits of a one-byte-sequence, the shift operators (<< and >>) move two of those bit sequences one block lower or higher, before the OR-operator (|) combines all three bit sequences into a single sequence that is then stored as a new number inside the colour variable.
  3. I see. Thank you for your explanation. I was really confused on that part of << and >>.