Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

javascript - Java中~~(“双波浪号”)有什么作用?(What does ~~ (“double tilde”) do in Javascript?)

I was checking out an online game physics library today and came across the ~~ operator.(我今天正在检查一个在线游戏物理库,遇到了~~运算符。)

I know a single ~ is a bitwise NOT, would that make ~~ a NOT of a NOT, which would give back the same value, wouldn't it?(我知道单个?是按位NOT,这会使~~变成NOT的NOT,这会返回相同的值,不是吗?)   ask by Shane Tomlinson translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It removes everything after the decimal point because the bitwise operators implicitly convert their operands to signed 32-bit integers.(它删除小数点后的所有内容,因为按位运算符会将其操作数隐式转换为带符号的32位整数。)

This works whether the operands are (floating-point) numbers or strings, and the result is a number.(无论操作数是(浮点)数字还是字符串,这都有效,并且结果是数字。)

In other words, it yields:(换句话说,它产生:)

function(x) {
  if(x < 0) return Math.ceil(x);
  else return Math.floor(x);
}

only if x is between -(2 31 ) and 2 31 - 1. Otherwise, overflow will occur and the number will "wrap around".(仅当x在-(2 31 )和2 31-1之间 。否则,将发生溢出并且数字将“环绕”。)

This may be considered useful to convert a function's string argument to a number, but both because of the possibility of overflow and that it is incorrect for use with non-integers, I would not use it that way except for "code golf" ( ie pointlessly trimming bytes off the source code of your program at the expense of readability and robustness).(将函数的字符串参数转换为数字可能被认为是有用的,但是由于存在溢出的可能性,并且不适合与非整数一起使用,因此我不以这种方式使用它,除了“ code golf”( 无意义地从程序源代码中修剪字节,以牺牲可读性和健壮性为代价)。)

I would use +x or Number(x) instead.(我会改用+xNumber(x) 。)

How this is the NOT of the NOT(NOT是NOT的NOT)

The number -43.2, for example is:(数字-43.2例如:)

-43.2 10 = 11111111111111111111111111010101 2 (-43.2 10 = 11111111111111111111111111010101 2)

as a signed (two's complement) 32-bit binary number.(作为有符号(二进制补码)的32位二进制数。)

(JavaScript ignores what is after the decimal point.) Inverting the bits gives:((JavaScript忽略小数点后的内容。)将这些位取反可以得到:)
NOT -43 10 = 00000000000000000000000000101010 2 = 42 10 (非-43 10 = 00000000000000000000000000101010 2 = 42 10)

Inverting again gives:(再次反转得到:)

NOT 42 10 = 11111111111111111111111111010101 2 = -43 10 (不是42 10 = 11111111111111111111111111010101 2 = -43 10)

This differs from Math.floor(-43.2) in that negative numbers are rounded toward zero, not away from it.(这与Math.floor(-43.2)不同之处在于负数四舍五入为零,而不是四舍五入。)

(The floor function, which would equal -44, always rounds down to the next lower integer, regardless of whether the number is positive or negative.)((等于-44的下限函数始终舍入为下一个较小的整数,而不管该数是正数还是负数。))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...