Why MySQL BIT_OR() is returning different value than PHP bitwise operation

Jaylen

I have the following data in a MySQL table

"Data Dump" 2 phone_calls 001 2 phone_calls 010 2 phone_calls 100 2 phone_calls 1000 2 phone_calls 10000 2 phone_calls 100000

if I run PHP code to do bitwise Or operation like so

echo bindec('001') | bindec('010') | bindec('100') | bindec('1000') | bindec('10000') | bindec('100000');

I get 63 for output "which is expected"

if I do the Or manually

000001
000010
000100
001000
010000
100000
======
111111

the result = 111111 which is `32 + 16 + 8 + 4 + 2 + 1 = 63`

When I run the following query in MySQL

SELECT user_id, section_name, BIT_OR(permission_type) AS final
FROM permissions
WHERE section_name ='phone_calls' and user_id = 2
GROUP BY user_id, section_name

which is basically running BIT_OR() on "Data Dump" listed above and the output is

 2  phone_calls 108543

why MySQL gives me 108543 and PHP gives me 63? How can I get MySQL to give me 63?

paxdiablo

108543 is what you get when you or together the decimal values {1, 10, 100, 1000, 10000, 100000}.

In other words, they're not being treated as binary values.

You either need to store the correct decimal values for the binary equivalents, { 1, 2, 4, 8, 16, 32}, or find a way to convert decimal variants holding only 0 and 1 digits into an appropriate value.

If you want to retain strings holding the bit pattern, they can be converted into decimal with something like:

conv(colname,2,10)

which does a base conversion:

mysql> select conv('10',2,10);
    -> '2'
mysql> select conv('1000',2,10);
    -> '8'

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Why MySQL BIT_OR() is returning different value than PHP bitwise operation

분류에서Dev

Why is AES function returning different value?

분류에서Dev

btrfs: why subvolid is different than value provided to mount?

분류에서Dev

Why do you have to cast a bitwise operation on a byte to a byte in Java?

분류에서Dev

PHP function not returning value

분류에서Dev

Bitwise operation: fitsBits function

분류에서Dev

Why is diff returning an incorrect value?

분류에서Dev

Why is the default value for max_features in RandomForestClassifier different than the one in RandomForestRegressor?

분류에서Dev

Mysql_num_rows not returning any value

분류에서Dev

Why is PHP if(!$_SESSION['username']) is returning false

분류에서Dev

mean of non zero elements - why are these two attempt returning different results?

분류에서Dev

Why are print_r and return returning different values?

분류에서Dev

Mysql php, Different tables for different project types

분류에서Dev

MySQL Random Select Query with limit returning different number of results (undesired)

분류에서Dev

Why does the MBR return a different offset than Ubuntu's commands?

분류에서Dev

Why is my GDM at a different TTY than my desktop environment?

분류에서Dev

Why the function pointer pointing to different function works than it is defined

분류에서Dev

Why is <hr> given a thickness in HTML different than CSS?

분류에서Dev

Why is a user checking a box different than doing it programmatically?

분류에서Dev

Why does my php returns one value (one row) from mysql?

분류에서Dev

Why does scala return an out of range value in this modulo operation?

분류에서Dev

PHP Foreach same key different value

분류에서Dev

Php split value into different li classes

분류에서Dev

How to implement bit vectors with bitwise operations?

분류에서Dev

Why is this not returning an @@ERROR?

분류에서Dev

why the value of (n) in big oh notation examples is different?

분류에서Dev

Mysql Syntax PHP add the value of 1

분류에서Dev

php mysql get 2 table value

분류에서Dev

Why is my constructed formula giving different results than the same formula as UDF in Excel?

Related 관련 기사

  1. 1

    Why MySQL BIT_OR() is returning different value than PHP bitwise operation

  2. 2

    Why is AES function returning different value?

  3. 3

    btrfs: why subvolid is different than value provided to mount?

  4. 4

    Why do you have to cast a bitwise operation on a byte to a byte in Java?

  5. 5

    PHP function not returning value

  6. 6

    Bitwise operation: fitsBits function

  7. 7

    Why is diff returning an incorrect value?

  8. 8

    Why is the default value for max_features in RandomForestClassifier different than the one in RandomForestRegressor?

  9. 9

    Mysql_num_rows not returning any value

  10. 10

    Why is PHP if(!$_SESSION['username']) is returning false

  11. 11

    mean of non zero elements - why are these two attempt returning different results?

  12. 12

    Why are print_r and return returning different values?

  13. 13

    Mysql php, Different tables for different project types

  14. 14

    MySQL Random Select Query with limit returning different number of results (undesired)

  15. 15

    Why does the MBR return a different offset than Ubuntu's commands?

  16. 16

    Why is my GDM at a different TTY than my desktop environment?

  17. 17

    Why the function pointer pointing to different function works than it is defined

  18. 18

    Why is <hr> given a thickness in HTML different than CSS?

  19. 19

    Why is a user checking a box different than doing it programmatically?

  20. 20

    Why does my php returns one value (one row) from mysql?

  21. 21

    Why does scala return an out of range value in this modulo operation?

  22. 22

    PHP Foreach same key different value

  23. 23

    Php split value into different li classes

  24. 24

    How to implement bit vectors with bitwise operations?

  25. 25

    Why is this not returning an @@ERROR?

  26. 26

    why the value of (n) in big oh notation examples is different?

  27. 27

    Mysql Syntax PHP add the value of 1

  28. 28

    php mysql get 2 table value

  29. 29

    Why is my constructed formula giving different results than the same formula as UDF in Excel?

뜨겁다태그

보관