Giter VIP home page Giter VIP logo

Comments (7)

williambryan777 avatar williambryan777 commented on August 23, 2024 9

记住这个规则:一个数加1然后再取相反数。
比如:

  • ~3就变成-(3+1)=-4
  • ~(-6)就是-(-6+1)=5

from frontend-interview.

WangXiaoyugg avatar WangXiaoyugg commented on August 23, 2024 2

位运算 NOT 由否定号(~)表示,它是 ECMAScript 中为数不多的与二进制算术有关的运算符之一
位运算 NOT 是三步的处理过程:
1.把运算数转换成 32 位数字
2.把二进制数转换成它的二进制反码
3.把二进制数转换成浮点数
位运算 NOT 实质上是对数字求负,然后减 1;4 >-4>-5

from frontend-interview.

Jemair avatar Jemair commented on August 23, 2024 2

@rodickmini 先把"4"隐式转换成Number型,再进行计算
~相当于取反 再减1
~4 = -5
~-5 = 5 - 1 = 4

参见红宝书40页 按位非

from frontend-interview.

gaoryrt avatar gaoryrt commented on August 23, 2024 1

记得哪里看的,非负数取整Math.floor()也可以使用~~()

from frontend-interview.

kevinwulong avatar kevinwulong commented on August 23, 2024

~操作符 转换成二进制后 取反数。
4:00000000000000000000000000000100 => 11111111111111111111111111111011 =>-5

from frontend-interview.

Jemair avatar Jemair commented on August 23, 2024

-5 按位非

from frontend-interview.

rodickmini avatar rodickmini commented on August 23, 2024

问:为什么~~"4"等于4

from frontend-interview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.