Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the JS bit operator

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of how to use the JS bit operator, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to use the JS bit operator. Let's take a look at it.

Bitwise and (AND) &

Convert the number to binary, and then convert it back to decimal

The binary representation of / / 1 is 00000000 00000000 00000000 00000001move / 3. The binary representation of /-/ 1 is 00000000000000000000000011. The binary representation of /-/ 1 is 000000000000000000000000 00000001console.log (1 & 3) / / 1 bitwise or (OR). |

Convert a number to binary, then perform an OR operation, and then convert back to decimal

The binary representation of / / 1 is 00000000 00000000 00000000 00000001move / 3. The binary representation of /-/ 3 is 00000000000000000000000011. The binary representation of /-/ 3 is 00000000000000000000000000 00000011console.log (1 | 3) / / 3 bitwise XOR (XOR) ^

Convert the number to binary, then XOR, and then back to decimal

The binary representation of / 1 is 00000000 00000000 00000000 00000001move / 3, the binary representation of 00000000000000000000000000011 is 00000000000000000000000011, the binary representation of /-/ 2 is 000000000000000000000000 00000010console.log (1 ^ 3) / 2 bitwise non-(NOT) ~

Convert the number to binary, then do non-operation, and then convert back to decimal, which is to find the binary inverse code.

The / 1 inverse binary is represented as 11111111 11111111 11111111 11111110 / because the first bit (symbol bit) is 1, so this number is negative. JavaScript represents a negative number in the form of complement, that is, you need to subtract 1 from this number, then reverse it again, and then add a negative sign to get the decimal value / / 1 corresponding to this negative number. The inverse code minus one is expressed as 11111111 11111111 11111111 11111101 swap / take the inverse 0000000000000000010 / expressed as-2console.log (~ 1) / /-2 shift to the left (Left shift) > 1) / 0 unsigned right >

Convert the number to binary, then discard the low bit, and fill in 0 on the left, so it is always non-negative.

For non-negative numbers, the signed right shift and the unsigned right shift are always equal.

The ingenious use of bit operator in JS to judge parity / / even number & 1 = 1console.log (2 & 1) / / 0console.log (3 & 1) / / 1 rounding console.log (~ 6.83) / / 6console.log (6.83 > > 0) / / 6console.log (6.83 > > 0) / / 6 commutative value var a = 6var b = 8a ^ = bb ^ = bconsole.log (a) / / 8console.log (b) / / 6RGB value and hexadecimal color value conversion function hexToRGB (hex: string): string {const hexx = hex.replace ('#' '0x') const r = hexx > > 16 const g = hexx > > 8 & 0xff const b = hexx & 0xff return `rgb (${r}, ${g}, ${b}) `} function RGBToHex (rgb: string): string {const rgbArr = rgb.split (/ [^\ d] + /) const color = rgbArr [1] | rgbArr [2]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report