Javascript what does the
This question already has an answer here:
It's a bitwise operator... to quote this page:
Bitwise operators treat their operands as a sequence of 32 bits (zeroes and ones), rather than as decimal, hexadecimal, or octal numbers. For example, the decimal number nine has a binary representation of 1001. Bitwise operators perform their operations on such binary representations, but they return standard JavaScript numerical values.
what happens, is that the operator treats the number as a 32 bit integer; so 5.123 is treated as:
0000 0000 0000 0000 0000 0000 0000 0101
(the decimal part is thrown out) and 0 is treated as
0000 0000 0000 0000 0000 0000 0000 0000
then the OR function compares the two, and writes a 1 if either number has a 1. So using the bitwise Or with a decimal number and zeeo is essentially a way to discard the decimal part and retain the integer part.
Your other example with two is:
0000 0000 0000 0000 0000 0000 0000 0101 (5)
0000 0000 0000 0000 0000 0000 0000 0010 (2)
--------------------------------------- ORed
0000 0000 0000 0000 0000 0000 0000 0111 (7)
and the example with 4:
0000 0000 0000 0000 0000 0000 0000 0101 (5)
0000 0000 0000 0000 0000 0000 0000 0100 (4)
--------------------------------------- ORed
0000 0000 0000 0000 0000 0000 0000 0101 (5)
You can use it to convert to discard the decimal part of a number - see Using bitwise OR 0 to floor a number
链接地址: http://www.djcxy.com/p/77438.html上一篇: 0“产生一个整数?
下一篇: Javascript是什么