Jump to content

Recommended Posts

I've been reading the O'Reilly Programming PHP 2nd edition.

It's coming up with the Bitwise operators and I can't get my head around the logic of them.

 

~ (tilde), this operator changes 1s to 0s and 0s to 1s in binary? Correct?

 

& (ampersand) .. confused by this one. Bascally it states that if you have octal 0755 and octal 0671, you can grab the binary of them and work out what octal 0651 is in binary.

Octal 0755 - Binary 111 101 101

Octal 0671 - Binary 110 111 001

Octal 0651 - Binary 110 101 001

Logic of this, is Octal 6 - 110, Octal 5 - 101, Octal 1 - 001

"wolf" & "cat" is "cad" I don't quite understand?

 

| (Vertical bar) - If both bits are 0, the resulting bit is 0; otherwise, the resulting bit is 1.

Example: 0755 | 020 is 0775, or another example: "pussy" | "cat" is "suwsy"

That's not relating to 0, or 1?

 

^ (caret) - If either of the bits in the pair, but not both, is 1 the resulting bit is 1; otherwise the resulting bit os 0.

Example: 0755 ^ 023 is 776, or another example: "big drink" ^ "AA" is "#("

 

<< (left shift) - Is this like decimal places, but only without the point?

example: 3 << 1 is 6

So that means, in binary 3 is 11, and it's shifted 1 place left. Causng a 0 to pop up at the end. Which 110 is the binary of 6.

3 << 2, would mean shifting 11 left two times, making 1100 .. which is the binary for 12? Correct? I checked 12 to binary in a binary to text converter. It came up with "0011000100110010" Confused?

 

>> (right shift) - Would be the same as << but shifting right instead of left.

 

P.S I dont understand these so a little help is appreciated :), the examples are from the book.

Not sure what you are expecting in a forum post. Not really the appropriate medium for an exposition on bitwise operators. However, I think the use of 'text' in the examples is confusing. I can't think of any useful implementation of bitwise operators with text. Most of the uses I have seen with bitwise operators are for storing/mainpulating multiple boolean values. That's not to say there aren't other valid uses, but sticking with numbers will make more sense I think.

 

Here is one example:

 

Let's say you have an application where users' permissions are set according to the group(s) they are assigned to. The permissions could be stored in a bitwise operator where each bit is a true/false for a specific permission.

 

Permissions:

CreateUser | DeleteUser | CreateProduct | DeleteProduce

 

So a user with the value 1010 (or 10 in binary) would have permissions to Create Users and Create Products, but would not be able to delete either. But, let's say users can be in multiple groups. How do you determine their permissions across all groups? Eaasy, just use the "|" (pipe). As you state above

| (Vertical bar) - If both bits are 0, the resulting bit is 0; otherwise, the resulting bit is 1.

 

So, if you use that operator on two values, you will get the combined permissions. So, if a user is in two groups with the permission values of 1010 and 1100, their combined permission value (using |) will be 1110

I think I understand yes.

As for '& (ampersand)'

Would that be turning two strings, or numbers to binary. Finding the difference, and either output it in binary, or in the numeric or string. Which is rather.. well I understand it. I just wouldn't know quite how to do it. The Octals given in examples I could. Though maybe 2 octals could be completely the opposite in binary. Causing it to be.. strange.

So again for lets say, user permissions - If they were multiple groups.. they could combine the two groups, but infact it wouldn't be actually combining the strings/numeric values. It'll be finding the middle of them.

 

So.. two groups, 4 and 6.

4 & 6 is 5, so for user permissions, if they have 2 groups, eg: 4 and 6, the user group they would actually be in, is whats provided in user group 5, which is pointless if someone was to do that, but just an example :)

 

If that makes sense? I can't explain properly. I'm assuming I got the right idea with << and >>, and possibly ~, so the only other one would be ^ to figure out.

Again, using ocals and words with respect to bitwise operators will be confusing if it is not in the context of a real-world example. Stick with binary and base 10 numbers and it will be easier to understand. If you have a need to work with text and/or octals with respect to bitwise operators you will have the understanding to apply them.

 

Ok, here is an example to explain a possible use for the ampersand '&' operator. This operator will return the bits that are set in both values. So, expanding upon the previous example, let's say in addition to the user's group permissions for general activity there could also exist permissions to specific records. So, in addition to having the general permission to delete staff, those staff can be put into security groups so only certain users can delete them. In that case you could use the & operator against the user's general permission and against the staff record specific permissions to ensure that the user has permissions to delete staf AND that the user has permissions to delete the specific staff. 0101 & 0110 = 0100

 

The carat '^' returns the bits where a bit is turned on in one value but not the other. I can't think of an example right now. But, basically it would be like this: 0110 ^ 0101 = 0011

 

Here is a tutorial that looks pretty good: http://www.litfuel.net/tutorials/bitwise.htm

I looked at the binary chart. (1 byte = 8 bits)

I tried to work out the number 54 in binary. I worked out as 00111000

Though I tried numerous binary converters and it comes up with 8?

 

Can someone confirm I'm doing the right job.

I know this isn't a PHP topic no longer. >.<

"where a bit is turned on in one value"

Dont understand what you mean by this?

 

0 = Off, 1 = On

 

A byte is mayde up of 8 bits. Something like 00110101. So, what is that? Each place has a value the same as a base 10 number. Ex. The number 125. The 5 is in the ones place, the 2 is in the 10's place and the 1 is in the hundredths place. Each "place" can have a value of 0 to 9. In binary each place can have a value of 0 or 1. The amount of each place (i.e. column) is different than base 10. In binary it works like this:

 

128 | 64 | 32 | 16 | 8 | 4 | 2 | 1

 

So, the binary number 00110110 has the bits "turned on" for 32, 16, 4, & 2. So, the equivalent base 10 number is 54. 00111000 would be 32 + 16 + 8 = 56.

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.