Ebrahim
Ebrahim

Reputation: 95

How to use binary/bitfield literals in a MySQL statement

select (bin(~'101010101010101')) as Result;

Result is : '1111111111111111101001000010000111000000110011111000000101001010'

Is it true?

I expect to see this result:

'1111111111111111111111111111111111111111111111111010101010101010'

Help me please.

Upvotes: 0

Views: 337

Answers (2)

Esailija
Esailija

Reputation: 140234

You are passing a string to a bitwise operation. These operations are defined for BIGINT integers in mysql, so you might get unexpected results.

You should try:

SELECT bin(~0x5555) as Result

or:

SELECT bin(~b'101010101010101' ) as Result

Results in:

'1111111111111111111111111111111111111111111111111010101010101010'

Upvotes: 1

Henrik
Henrik

Reputation: 23324

Looks like 101010101010101 is interpreted as decimal number. Its binary representation is 10110111101111000111111001100000111111010110101. Complement this and you get the printed result.

Upvotes: 0

Related Questions