Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to check if bit field is turn on in php

What is the correct way to check if bit field is turn on - (in php) ?

I want to check a bit field that come from db(mysql) if is turn on or not.

is this is the correct way ?

if($bit & 1)

Are there other ways ?

I see somebody code that using ord() function , it is correct ?

like if(ord($bit) == 1)

like image 902
Haim Evgi Avatar asked Nov 28 '22 19:11

Haim Evgi


1 Answers

Use

if( $bit & (1 << $n) ) {
  // do something
}

Where $n is the n-th bit to get minus one (for instance, $n=0 to get the least significant bit)

like image 115
Frxstrem Avatar answered Dec 04 '22 22:12

Frxstrem