Register for your free account! | Forgot your password?

Go Back   elitepvpers > Blogs > manulaiko3.0
You last visited: Today at 13:36

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Rate this Entry

Programming in PHP - Bitwise Operators

Posted 01/30/2015 at 16:10 by manulaiko3.0

Copy and paste from my website:

I’m assuming that you know binary, if you don’t, read the next chapter or learn binary somewhere else (maybe I do a tutorial) and come back here
We have covered mostly all kind of operators, we just need to take a look at bitwise operators and no more operators since next chapter
I want to say that bitwise operators are rarely used in PHP since it works at binary level (10101110101101010101101010101111101010100010101.. ) so I don’t think that you will use them but if you see them anywhere you will know what they means and what they do.
This chapter will be funny/hard, since the most of the post will be 1001110110 and it might be a bit difficult to understand, but play with bits is kinda funny
So, bassically, bitwise operators are used to play with bits, you can make logic coparations at bit level, move bits to left or right… They’re rarely used but they’re the same in any other programming language. There 6 types of bitwise operators:
  • And (&)
  • Or (|)
  • Xor (^)
  • Not (~)
  • Shift left (<<)
  • Shift right (>>)
And (&)

So, you can think in this as the comparation operator and (&&), just think that 1 = true, 0 = false and 2 = trulse.
We have number 15 and 7, in binary this is 00001111 and 00000111
00001111 00000111 Let’s parse this to boolean:
false false false false true true true true false false false false false true true true Now we can do like a normal and comparation:
false false false false true true true true false false false false false true true true -------------------------------------------- false false false false false true true true Now in binary:
00001111 00000111 -------- 00000111 Easy right?
Let’s look at the php code:
<?php $var1 = 15; $var2 = 7; $var3 = $var1 & $var2; echo $var3; //7 And that’s all!
Or (|)

Now you know how does & works you can easy understand |, the example is the same.
We have number 15 and 7, in binary this is 00001111 and 00000111
00001111 00000111 Let’s parse this to boolean:
false false false false true true true true false false false false false true true true Now we can do like a normal and comparation:
false false false false true true true true false false false false false true true true -------------------------------------------- false false false false true true true true Now in binary:
00001111 00000111 -------- 00001111 Easy right?
Let’s look at the php code:
<?php $var1 = 15; $var2 = 7; $var3 = $var1 | $var2; echo $var3; //15 Xor (^)

This can be a bit(*insert laugh here*) confusing but it’s pretty easy.
Xor = Exclusive OR
By knowing what means the name you can understand that it will be like a normal OR, the difference is that when both bits are 1 (true) XOR returns 0 (false), let’s look at same example:
We have number 15 and 7, in binary this is 00001111 and 00000111
00001111 00000111 Let’s parse this to boolean:
false false false false true true true true false false false false false true true true Now we can do like a normal and comparation:
false false false false true true true true false false false false false true true true ----------------------------------------------- false false false false true false false false Now in binary:
00001111 00000111 -------- 00001000 Easy right?
Let’s look at the php code:
<?php $var1 = 15; $var2 = 7; $var3 = $var1 ^ $var2; echo $var3; //8 Not (~)

Imagine you want to search for bits that are in var1 and not in var2, or in var2 and not in var1. We can now use the not operator, which is represented by a tilda (~).
<?php $var1 = 15; $var2 = 7; $var3 = $var1 & ~$var2; echo $var3; //8 In this specific example, we can see that the only bit set in var1 and not var2 is again fourth from the right, which is 8.
Shift left (<<)

This operator (and the next) is easier to see like a multiplication.
We have the number 11 and we’re going to shift it 2 bits to left.
Shift bits to left is the same than multiply, the example would be something like this:
11 * 2^2 2^2 means that 2 is powed to 2, it’s not a Xor
Now let’s look at the bits:
00001011 -------- 00101100 This would be in php:
<?php $var1 = 11; $var2 = 2; $var3 = $var1 << $var2; echo $var3; //44 Shift right (>>)

If shift left means multiply, shift right means divide? YES!
Here's the an example:
We have the number 52 and we're going to shift it 2 bits to right.
Shift bits to right is the same than divide, the example would be something like this:
52 / 2^2 2^2 means that 2 is powed to 2, it's not a Xor
Now let's look at the bits:
00110100 -------- 00001101 This would be in php:
<?php $var1 = 52; $var2 = 2; $var3 = $var1 << $var2; echo $var3; //13 Well, that was everything about bitwise operators, no more operators untill next chapter: Control Structures
See you!
Posted in Uncategorized
Views 587 Comments 0 Email Blog Entry
« Prev     Main     Next »
Total Comments 0

Comments

 

All times are GMT +1. The time now is 13:37.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.