› MySQL 5.5 Community Server
› MySQL 5.6 Community Server
› Percona Configuration Wizard
› XtraBackup 搭建主从复制
Great Sites on MySQL
› Percona
› MySQL Performance Blog
› Severalnines
推荐管理工具
› Sequel Pro
› phpMyAdmin
推荐书目
› MySQL Cookbook
MySQL 相关项目
› MariaDB
› Drizzle
参考文档
› http://mysql-python.sourceforge.net/MySQLdb.html
gyinbj
V2EX  ›  MySQL

求助 mysql & 运算符是什么意思, 怎样解释

  •  
  •   gyinbj · Apr 8, 2020 · 5448 views
    This topic created in 2403 days ago, the information mentioned may be changed or developed.

    select id from Table where id = '888888' and (ext_type & 4 = 4 )

    这句话后面的(ext_type & 4 = 4 ) 要怎样解释?

    数据存储是 1 2 4 8 的和 15

    用于判断 1 2 4 8 是否符合这 4 个权限

    9 replies  •  2020-04-09 10:59:15 +08:00
    Presbyter
    Presbyter
    Apr 8, 2020
    1
    关键词“8421 码”,“逻辑运算”.
    gyinbj
    gyinbj
    OP
    Apr 8, 2020
    2
    @Presbyter 没太懂。
    PTLin
    PTLin
    Apr 8, 2020
    3
    判断第三位是否被置位
    no1xsyzy
    no1xsyzy
    Apr 8, 2020
    4
    位运算作 bit test
    老的 C 甚至是 asm 的做法了
    caola
    caola
    Apr 8, 2020
    5
    位运算符:& 按位与, | 按位或, ^ 按位异或
    otakustay
    otakustay
    Apr 8, 2020
    6
    这是 bit enum 或者 flag enum,搜一下能找到比较详尽的资料
    dapang1221
    dapang1221
    Apr 8, 2020
    7
    那个 4 换成二进制是 0100,和 ext_type 做与运算,相当于把 ext_type 的第三个 bit 位提取出来,再判断这一位是不是 1

    不过 mysql 位运算会全表扫描,这条 sql 有限定 id 了倒是不会全表扫
    shaoyijiong
    shaoyijiong
    Apr 8, 2020
    8
    字面含义二进制计算 得到的结果十进制后再比较
    0100 (4)
    &
    0100 (4)
    -----
    0100 (4)
    Aresxue
    Aresxue
    Apr 9, 2020
    9
    把十进制转成二进制就看出规律了, 0100,相当于取第三位, 这个做法比较老但还算常见, linux 的常用权限 755, java 里面对于修饰符的判断都有用到
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1847 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 01:10 · PVG 09:10 · LAX 17:10 · JFK 20:10
    ♥ Do have faith in what you're doing.