DanDaBeginner Posted August 16, 2007 Share Posted August 16, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/65209-solved-bitrwise-operator/ Share on other sites More sharing options...
Orio Posted August 16, 2007 Share Posted August 16, 2007 I personally never had the need to use them when programming in php, but I guess they can be used for encoding/decoding stuff, and in other mathematical stuff. When I used assembly I used them alot. Orio. Quote Link to comment https://forums.phpfreaks.com/topic/65209-solved-bitrwise-operator/#findComment-325609 Share on other sites More sharing options...
tibberous Posted August 16, 2007 Share Posted August 16, 2007 Bitwise operator is really only used for encryption and for bitsets, which let you put a bunch of binary settings in a single variable. You can also use the bit operator as an or that doesn't short, same for and. Quote Link to comment https://forums.phpfreaks.com/topic/65209-solved-bitrwise-operator/#findComment-325612 Share on other sites More sharing options...
DanDaBeginner Posted August 16, 2007 Author Share Posted August 16, 2007 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... Quote Link to comment https://forums.phpfreaks.com/topic/65209-solved-bitrwise-operator/#findComment-325617 Share on other sites More sharing options...
tibberous Posted August 16, 2007 Share Posted August 16, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/65209-solved-bitrwise-operator/#findComment-325618 Share on other sites More sharing options...
zq29 Posted August 16, 2007 Share Posted August 16, 2007 http://www.phpfreaks.com/forums/index.php/topic,113143.msg459615.html#msg459615 Quote Link to comment https://forums.phpfreaks.com/topic/65209-solved-bitrwise-operator/#findComment-325620 Share on other sites More sharing options...
HuggieBear Posted August 16, 2007 Share Posted August 16, 2007 Very useful for security permissions. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/65209-solved-bitrwise-operator/#findComment-325621 Share on other sites More sharing options...
tibberous Posted August 16, 2007 Share Posted August 16, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/65209-solved-bitrwise-operator/#findComment-325628 Share on other sites More sharing options...
ToonMariner Posted August 16, 2007 Share Posted August 16, 2007 I use them for permissions - its exactly the same system used for your dir/file permissions on a server http://www.phpfreaks.com/tutorials/151/0.php Quote Link to comment https://forums.phpfreaks.com/topic/65209-solved-bitrwise-operator/#findComment-325649 Share on other sites More sharing options...
DanDaBeginner Posted August 16, 2007 Author Share Posted August 16, 2007 thanks guys... Quote Link to comment https://forums.phpfreaks.com/topic/65209-solved-bitrwise-operator/#findComment-325650 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.