1

I have a binary(100), and I want to bitwise OR just one of its bytes with a constant.

Any idea how this would be done?

Alternatively, how can I store a value into a byte of a binary(100)?

6
  • @Alnitak: true, the functions have a 64bit limit. but nothing says you can't do string manipulations/casting to get at whatever byte/bit you want in a larger field. Commented Jun 27, 2011 at 21:05
  • @Nerfino what are you trying to store? Does your data really need to be 100 contiguous bytes (would 13 eight-byte fields do?) Are you aware of the space trimming "feature" in binary fields that could well mess up your bit fields? Commented Jun 27, 2011 at 21:09
  • What space trimming feature??? Commented Jun 27, 2011 at 21:19
  • @Nerfino see last three paras of dev.mysql.com/doc/refman/4.1/en/binary-varbinary.html Commented Jun 27, 2011 at 21:24
  • @Nerfino -actually that might only be an issue in older versions. It seems to have changed between 4.1 and 5.x Commented Jun 27, 2011 at 21:26

1 Answer 1

2

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.

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)
)

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.