Jump to content

Recommended Posts

I dont really understand the purpose of bitwise operator... (such as : ^,|,~<<,>>)

 

when can it be useful and what do they do? I tried reading some tutorial and the php manual..but Im really confuse..please somebody adjust the screw on my brain...it seems to be loosing...

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/65209-solved-bitrwise-operator/
Share on other sites

thank you tibberous,

 

thank you Orio, even if the need of bitwise is not that much.. i really want to grab the concept behind it.. explaing it to me will be helpful...

 

bitsets? binary setting in a single variable? what is that? sorry for my ignorance but I really like to know this one...

Some languages short ors and ands, not sure if php does.

 

It works like this:

 

Lets say you have an if statement:

 

if($word1 == 'true' && $word2 == 'false')

 

Now, if $word1 is not equal to 'true', the if is false, regardless of $word2. Rather than calculate the irrelevant $word2 == 'false', it just skips it and evaluates the if to false. In this case it doesn't matter, but if the line was something like:

 

if($word1 == 'true' && ($words--)>0)

 

then shorting the if would keep $words-- from happening if $word1 was not equal to 'true'. Makes for a bitch of a runtime error, and using ^ in place of && keeps it from happening....

 

I think...

 

Might just be C++...

 

or Java...

 

Try it :)

 

Also, I don't think there is any guarantee the conditions are evaluated in order, and it might be there evaluated in reverse.

My research has found that php does short expressions, that ^ and | keeps it from doing that, and that it seems to go from left to right, but that might not be guaranteed.

 

And ^ ands bits, which is like adding them, except it doesn't carry. So...

 

1010 ^

1100 =

0110

 

You kind of have to know a little bit about binary, it's hard to explain online.

 

It isn't used much. You only have to use it if it's built into a 3rd party library, and even then you don't really have to understand it.

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.