search

Home  >  Q&A  >  body text

javascript - Solve the following code. . How to understand?

color='#FFFFFF';
var colorVal = parseInt(color.slice(1), 16);
result = {
    red: colorVal >> 16 & 0xFF,
    green: colorVal >> 8 & 0xFF,
    blue: colorVal & 0xFF,
}
//{red:255,green:255,blue:255}

Understood>>Signed right shift, why do you need parseInt(,16)? >>Isn’t it right shift of binary data? . Then parseInt no matter what base it is converted into. The binary data should remain unchanged.
Why is it moved by 16 bits. . Isn't 'FFFFFF' 6bytes 48bit? .

大家讲道理大家讲道理2775 days ago459

reply all(2)I'll reply

  • ringa_lee

    ringa_lee2017-05-19 10:17:40

    parseInt('hexStr', 16) This sentence means to convert a string containing hexadecimal numbers into an Int numerical type by parsing hexadecimal. Only numerical types can be shifted.

    reply
    0
  • PHP中文网

    PHP中文网2017-05-19 10:17:40

    Why parseInt(,16)

    'FFFFFF' is just a string, and the right shift operation requires numbers.

    Then parseInt no matter what base it is converted into. The binary data should remain unchanged.

    Take 'FFFFFF' as an example. There is no F below hexadecimal, so it is NaN. The carry is different for values ​​above hexadecimal, so they are also different values.

    Why mobile 16-bit

    Each RGB color value is 0~255, which occupies 8 bits. To get the R value, shift right by 16 bits and remove G and B

    reply
    0
  • Cancelreply