I have a mySQL table with multiple columns (tinyint) with 0 or 1 values

I want to convert all these values into bits in a single integer column So a column named column1 will go to MSB another column will set 2nd bit etc...

How do I do it?

  • can you explain better? I don't get your problem
    – jcho360
    Commented May 17, 2012 at 14:52

1 Answer 1


Use logical shifts << plus a bitwise or | operation.

For example:

SELECT (col1<<16) | (col2<<15) | (col3<<14) | (col4<<13) ...

MySQL docs are here.

  • As long as a user remembers the order of the columns processed and has no need to do any mass querying of a single bit, this look like a good solution. +1 Commented May 17, 2012 at 16:23
  • You mean bitwise OR | operator. Thanks ! that was helpful
    – Nir
    Commented May 17, 2012 at 16:39

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.