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 |
    +------+------+------+
  • 相关阅读:
    ubuntu环境下快速搭建开发环境
    Ubuntu安装mysql及设置远程访问方法
    lua 获取指定目录下指定后缀文件名
    DLL远程注入及卸载实现
    c字符检测函数
    数据库bcp导入导出批处理工具
    Schtasks命令详解(计划任务DOS批处理)
    lmathlib文件
    Github常用命令【转】
    Github上传代码菜鸟超详细教程【转】
  • 原文地址:https://www.cnblogs.com/zengkefu/p/5619010.html
Copyright © 2011-2022 走看看