DAILY DOCDAILY DOC
Rust
Node
Notes
Ubuntu
Leetcode
  • it-tools
  • excalidraw
  • linux-command
Rust
Node
Notes
Ubuntu
Leetcode
  • it-tools
  • excalidraw
  • linux-command
  • BFC 块级格式化上下文
  • Note
  • WebAssembly
  • public api
  • 位运算
  • bitwise operator
  • css实现隐藏效果
  • css snippets
  • 抖音点赞
  • js 相等判断
  • fetch ReadableStream
  • git
  • Github Actions 工作流
  • google search
  • RPC vs HTTP
  • gravatar
  • hhkb
  • Init project
  • input 文件上传
  • mac

    • Mac 使用技巧
    • alfred
    • mac shortcuts
    • shortcuts text edit
    • mac 修改host
  • 微前端
  • mock
  • nginx dump
  • nginx
  • NirCmd
  • npm
  • Operator Precedence
  • package.json
  • url query 解析
  • pnpm
  • JavaScript Precise countdown
  • react 模版
  • regexp
  • setup web development
  • telegram

    • telegram bot
  • timeFunction ease
  • 视频裁剪
  • vscode

    • vscode 高级指南
    • bracketPairs
    • jsconfig.json
    • vscode pipe into code
    • social project
    • vscode tasks
  • draggable resizable
  • windows 激活
  • 前端截图实现
  • 文本配音 富文本实现
  • 图片处理
  • 前端坐标
  • 定时任务
  • work efficient
  • 微信小程序动画实现方案
  • 排列组合
  • 数列
  • 语音驱动文字
  • 浏览器
  • 状态管理
  • 移动盒子
  • 移动端开发常用snippets
  • 设计模式
  • web performance

位运算

  1. 按位与 (AND) 运算符 &

    • 操作:两个位都为 1 时,结果为 1,否则为 0。
    • 示例:
      let a = 5; // 0101
      let b = 3; // 0011
      console.log(a & b); // 0001 => 1
      
  2. 按位或 (OR) 运算符 |

    • 操作:两个位中有一个为 1,结果为 1,否则为 0。
    • 示例:
      let a = 5; // 0101
      let b = 3; // 0011
      console.log(a | b); // 0111 => 7
      
  3. 按位异或 (XOR) 运算符 ^

    • 操作:两个位中有且只有一个为 1,结果为 1,否则为 0。
    • 示例:
      let a = 5; // 0101
      let b = 3; // 0011
      console.log(a ^ b); // 0110 => 6
      
  4. 按位非 (NOT) 运算符 ~

    • 操作:按位取反,将 0 变为 1,1 变为 0。
    • 示例:
      let a = 5; // 0101
      console.log(~a); // 1010 => -6 (注意是负数,因为二进制补码表示法)
      
  5. 左移 (Left Shift) 运算符 <<

    • 操作:将位左移指定的次数,高位丢弃,低位补 0。
    • 示例:
      let a = 5; // 0101
      console.log(a << 1); // 1010 => 10
      
  6. 右移 (Right Shift) 运算符 >>

    • 操作:将位右移指定的次数,符号位不变,高位补符号位(保持符号)。
    • 示例:
      let a = 5; // 0101
      console.log(a >> 1); // 0010 => 2
      let b = -5; // 11111011 (补码表示)
      console.log(b >> 1); // 11111101 => -3
      
  7. 无符号右移 (Unsigned Right Shift) 运算符 >>>

    • 操作:将位右移指定的次数,高位补 0,不考虑符号位。
    • 示例:
      let a = 5; // 0101
      console.log(a >>> 1); // 0010 => 2
      let b = -5; // 11111011 (补码表示)
      console.log(b >>> 1); // 01111101 => 2147483645
      

使用情景实现

  1. 判断奇偶性

    • 场景:快速判断一个数是奇数还是偶数。
    • 实现:
      function isOdd(num) {
          return (num & 1) === 1;
      }
      console.log(isOdd(5)); // true
      console.log(isOdd(4)); // false
      
  2. 交换两个变量的值(不使用临时变量)

    • 场景:在算法中快速交换两个变量的值。
    • 实现:
      let a = 5,
          b = 3;
      a = a ^ b;
      b = a ^ b;
      a = a ^ b;
      console.log(a, b); // 3, 5
      
  3. 清除最低位的 1

    • 场景:在位操作算法中,快速清除一个数的最低位的 1。
    • 实现:
      function clearLowestBit1(num) {
          return num & (num - 1);
      }
      console.log(clearLowestBit1(10)); // 8 (1010 -> 1000)
      
  4. 获取一个数的绝对值

    • 场景:快速获取一个整数的绝对值。
    • 实现:
      function absoluteValue(num) {
          const mask = num >> 31;
          return (num + mask) ^ mask;
      }
      console.log(absoluteValue(-5)); // 5
      console.log(absoluteValue(5)); // 5
      
  5. 计算两个数的平均值(防止溢出)

    • 场景:在处理大整数时,防止溢出。
    • 实现:
      function average(x, y) {
          return (x & y) + ((x ^ y) >> 1);
      }
      console.log(average(10, 20)); // 15
      console.log(average(11, 21)); // 16
      
  6. 位掩码

    • 场景:使用位掩码来启用或禁用特定位。

    • 实现:

      const FLAG_A = 1 << 0; // 0001
      const FLAG_B = 1 << 1; // 0010
      const FLAG_C = 1 << 2; // 0100
      
      let flags = 0;
      flags |= FLAG_A; // 打开 FLAG_A
      flags |= FLAG_C; // 打开 FLAG_C
      console.log(flags); // 0101
      
      // 检查 FLAG_B 是否打开
      if (flags & FLAG_B) {
          console.log('FLAG_B is set');
      } else {
          console.log('FLAG_B is not set');
      }
      
Last Updated:
Contributors: rosendo
Prev
public api
Next
bitwise operator