Bit data type
Posted zengkefu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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‘
or value
‘0b
notation. value
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 | +------+------+------+
以上是关于Bit data type的主要内容,如果未能解决你的问题,请参考以下文章
Java 基本数据类型 Primitive Data Types
Java 基本数据类型 Primitive Data Types