zoukankan      html  css  js  c++  java
  • Bit data type

    mysql> create table t(a bit(4));
    Query OK, 0 rows affected (0.04 sec)
    
    
    mysql> insert into t select b'1000'; 
    Query OK, 1 row affected (0.18 sec)
    Records: 1  Duplicates: 0  Warnings: 0
    
    mysql> select * from t;
    +------+
    | a    |
    +------+
    |    |
    +------+
    1 row in set (0.00 sec)
    
    mysql> select hex(a) from t;
    +--------+
    | hex(a) |
    +--------+
    | 8      |
    +--------+
    1 row in set (0.00 sec)

    Bit-field values can be written using b'value' or 0bvalue notation. value is a binary value written using zeros and ones.

    Bit-field notation is convenient for specifying values to be assigned to BIT columns:

    mysql> CREATE TABLE t (b BIT(8));
    mysql> INSERT INTO t SET b = b'11111111';
    mysql> INSERT INTO t SET b = b'1010';
    mysql> INSERT INTO t SET b = b'0101';
    

    Bit values are returned as binary values. To display them in printable form, add 0 or use a conversion function such as BIN(). High-order 0 bits are not displayed in the converted value.

    mysql> SELECT b+0, BIN(b+0), OCT(b+0), HEX(b+0) FROM t;
    +------+----------+----------+----------+
    | b+0  | BIN(b+0) | OCT(b+0) | HEX(b+0) |
    +------+----------+----------+----------+
    |  255 | 11111111 | 377      | FF       |
    |   10 | 1010     | 12       | A        |
    |    5 | 101      | 5        | 5        |
    +------+----------+----------+----------+
    

    Bit values assigned to user variables are treated as binary strings. To assign a bit value as a number to a user variable, use CAST() or +0:

    mysql> SET @v1 = 0b1000001;
    mysql> SET @v2 = CAST(0b1000001 AS UNSIGNED), @v3 = 0b1000001+0;
    mysql> SELECT @v1, @v2, @v3;
    +------+------+------+
    | @v1  | @v2  | @v3  |
    +------+------+------+
    | A    |   65 |   65 |
    +------+------+------+
  • 相关阅读:
    redis相关
    Ubuntu安装之python开发
    Shell编程实战
    saltstack高效运维
    docker网络
    docker入门
    python学习博客地址集合。。。
    vue+uwsgi+nginx部署路飞学城
    部署你的CRM程序
    Java Netty教程(目录)
  • 原文地址:https://www.cnblogs.com/zengkefu/p/5619010.html
Copyright © 2011-2022 走看看