问题描述
我有一个binary(100)
,我想对它的一个字节与一个常量进行按位或运算.
I have a binary(100)
, and I want to bitwise OR just one of its bytes with a constant.
知道要怎么做吗?
或者,如何将值存储到binary(100)
的字节中?
Alternatively, how can I store a value into a byte of a binary(100)
?
推荐答案
首先,考虑BINARY
是否实际上是适当的字段类型.与BLOB
相比,它具有剥离尾随空格的潜在讨厌"特征. BINARY
实际上只是设计为不区分大小写的二进制文本字符串,而不是任意二进制数据的斑点.
Firstly, consider whether BINARY
is actually the appropriate field type. When compared to BLOB
it has a potentially nasty "feature" of stripping trailing spaces. BINARY
is really designed to be just a case-insenstive binary text string, and not a blob of arbitrary binary data.
如果确实使用blob,则需要结合使用SUBSTRING()
运算符和ASCII()
来提取所需的字节,然后使用|
逐位运算符.
If you do use a blob, you'd need to use the SUBSTRING()
operator combined with ASCII()
to extract just the byte you want, then use the |
bitwise operator.
要在第二个字节中设置内容,您需要使用类似以下内容的内容:
To set something in the second byte you'd need to use something like:
UPDATE TABLE SET col = CONCAT(
SUBSTR(col, 1, 1),
CHAR(ASCII(SUBSTR(col, 2, 1) | 0x80)),
SUBSTR(col, 3)
)
一个可能更简单的解决方案是将100个字节视为12.5个64位(即BIGINT
),然后对单个单词使用直接按位运算.
A possibly simpler solution might be to treat your 100 bytes as 12.5 lots of 64 bits (i.e. BIGINT
), and then use direct bitwise operations on individual words.
这篇关于如何按位或为二进制(100)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!