Is there a way to get the first active bit through SQL (MySQL)?
I have a column named a
, and a column named b
.
I want to fill with an UPDATE ... SET b = ...
query the b column, so that it contains the first bit 开发者_开发百科that a
has set to 1. Okay, you probably didn't understand, it's much easier to understand with an example:
a = 2508 = 0x9CC = 0100111001100
b = 4 = 0x4 = 0000000000100
a = 2080 = 0x820 = 0100000100000
b = 32 = 0x20 = 0000000100000
Is there a way to do this in pure SQL?
This should do it:
update your_table
set b = if(a > 0,pow(2,instr(reverse(bin(a)),'1')-1),0);
精彩评论