Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
can you explain better? I don't get your problem – jcho360 May 17 '12 at 14:52
up vote 2 down vote accepted

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

For example:

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

MySQL docs are here.

share|improve this answer
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 – RolandoMySQLDBA May 17 '12 at 16:23
You mean bitwise OR | operator. Thanks ! that was helpful – Nir May 17 '12 at 16:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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