2

I'm doing these comparisons to see what options are enabled but the PHP is returning wrong values. I'm exactly checking what banks were enabled by the user.

Próprio = 1
Itaú = 2
Bradesco = 4
Santander = 8
Caixa Econômica Federal = 16
HSBC = 32

When I load the data from MySQL, I've get 16, so only Caixa is turned on, but PHP says that Próprio and HSBC it turned on too. I've made other tests and I received the following results:

1 & 1 = true
1 & 2 = false
1 & 4 = false
1 & 8 = false
1 & 16 = true
1 & 32 = true

2 & 1 = false
2 & 2 = true
2 & 4 = false
2 & 8 = false
2 & 16 = false
2 & 32 = true

4 & 1 = false
4 & 2 = false
4 & 4 = true
4 & 8 = false
4 & 16 = false
4 & 32 = false

8 & 1 = false
8 & 2 = false
8 & 4 = false
8 & 8 = true
8 & 16 = false
8 & 32 = false

16 & 1 = true
16 & 2 = false
16 & 4 = false
16 & 8 = false
16 & 16 = true
16 & 32 = true

32 & 1 = true
32 & 2 = true
32 & 4 = false
32 & 8 = false
32 & 16 = true
32 & 32 = true

Thanks in advance for any help.

3
  • 2
    You must be doing something else wrong: codepad.org/Lr9TTAJe In which format do you save the data in the DB? Commented May 4, 2011 at 18:53
  • 3
    Please show us the actual code you are using. Commented May 4, 2011 at 18:54
  • 1
    Cant reproduce: var_dump(1 & 32); int(0). Whats the exact code you use? Commented May 4, 2011 at 18:55

3 Answers 3

4

Try casting both values to int before the bitwise operator. It seems that "1" & "16" = 1 while "1" & "2" = 0.

EDIT: The reason for this is that "If both the left-hand and right-hand parameters are strings, the bitwise operator will operate on the characters' ASCII values" as described in the manual.

EDIT 2: A quick test running bitwise operations against values cast to strings seems to yield results consistent with yours:

"1" & "1" == true
"1" & "2" == false
"1" & "4" == false
"1" & "8" == false
"1" & "16" == true
"1" & "32" == true

"2" & "1" == false
"2" & "2" == true
"2" & "4" == false
"2" & "8" == false
"2" & "16" == false
"2" & "32" == true

"4" & "1" == false
"4" & "2" == false
"4" & "4" == true
"4" & "8" == false
"4" & "16" == false
"4" & "32" == false
Sign up to request clarification or add additional context in comments.

Comments

2

As others have already commented, what you present in your question is hard to reproduce and the results are to be questioned because following the rules/definition of bitwise comparison in PHP (PHP bitwise comparison operators) there must/should be other results.

Keep in mind that there is a operator precedence. Put the bitwise operation into parenthesis to get your desired result:

(32 & 1)

As you did not shown any code I'm not totally sure if that is your problem.

Edit

As Kaivosukeltaja pointed out: The bitwise operator works on integers. You might want to cast you variables / values to integers first to be on the safe side:

( (int) 32 & (int) 1 )

This example is superfluos just to make visible what I'm writing about.

Comments

0

PHP acts strangely over what is true and false.

Try using 2 & 2 === 2 (=== not ==) and see if that works better.

2 Comments

Where is it acting strange? Every number different from zero will be true and 0 will be false.
With bit operators the loose comparison acts exactly, like its usually expected: if ($bitmask & pow(2,$i)) { echo "$i-th Bit set"; }

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.