Jump to content
Sign in to follow this  
acondef

What does these conditionals means? ($i < (1 << $len)) … ($i & (1 << $j))

Recommended Posts

Hello,

 

I am trying to understand a PHP code but it use a form of grouping conditionals that I do not know and I cannot find in the documentation:

 

for ($i = 1; $i < (1 << $len); $i++){...}

 

if ($i & (1 << $j)){...}

 

Any one know what is the normal form of these conditionals with && and ||?

 

Thanks!!!

Share this post


Link to post
Share on other sites

Those aren't conditionals, they're bitwise operators

 

<< will move bits of the left operand to the left by the amount specified on the right operand.

 

1 << $len is equivalent to 1 * pow(2, $len) (see pow() ) but much more efficient because computers operate in base 2 naturally.

 

The & operator operates in binary. It compares each 0 or 1 from a binary number with the 0 or 1 from another binary number and the result has a 1 if both of them were 1, 0 if not.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...