Understanding bitwise code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

That is because in php an unsigned integer is usually 32bits (thus 16 bits for positive numbers)... And your array is already using these.. 2^16 = 32768...
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

So there shouldn't be a problem then, right?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Timvw, you said it's an un-signed 32 bit integer but if it is positive it is only 16 bits, how that work? If it's 32 bits un-signed (meaning positive numbers) couldn't it go to 2^32 ?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Now I'm getting confused.

Unsigned is 32 bits yes. Signed is *not* 16 bits last I checked... it's still 32 bits but one of those bits is the sign bit so the number is smaller.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

It's not really a sign bit.. In that case you would end up with 2 representations for 0 ;)) It's 2-complement representation... But there are only 2^16 positive numbers (and null) that you can represent with 32 bits. (See also: http://www.php.net/int).
The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). PHP does not support unsigned integers.
Thus, the problem is that, when you add another security option, you don't know for sure what the result will be.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

timvw wrote:It's not really a sign bit.. In that case you would end up with 2 representations for 0 ;)) It's 2-complement representation... But there are only 2^16 positive numbers (and null) that you can represent with 32 bits. (See also: http://www.php.net/int).
The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). PHP does not support unsigned integers.
Thus, the problem is that, when you add another security option, you don't know for sure what the result will be.
My bad :oops: Hey I'm still a n00b and everybody knows it :P :lol:
sheila
Forum Commoner
Posts: 98
Joined: Mon Sep 05, 2005 9:52 pm
Location: Texas

Post by sheila »

d11wtq, don't give up so easy.
timvw wrote:But there are only 2^16 positive numbers (and null) that you can represent with 32 bits.
No, with 32 bits you can represent the values -2^31 to (2^31)-1 or -2,147,483,648 to 2,147,483,647
One bit is the sign and the other 31 are the value.

And that's what was quoted from the manual
(See also: http://www.php.net/int).
The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). PHP does not support unsigned integers.
Thus, the problem is that, when you add another security option, you don't know for sure what the result will be.
From the small amount of code shown I think it's safe to add another power of two to the array.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

sheila wrote:d11wtq, don't give up so easy.
timvw wrote:But there are only 2^16 positive numbers (and null) that you can represent with 32 bits.
No, with 32 bits you can represent the values -2^31 to (2^31)-1 or -2,147,483,648 to 2,147,483,647
One bit is the sign and the other 31 are the value.
(i was mistaken) You're right that one can represent 2^31 -1 positive numbers with 32bits.
(you are mistaken) If one bit is the sign then you end up with -0 and +0 .

((http://en.wikipedia.org/wiki/Two%27s_complement)
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

timvw wrote:If one bit is the sign then you end up with -0 and +0 .
You're right. But then again, it looks like there's plenty of room to add another permission, because summing all the permissions up (=43148) and adding 65536 (next permission value) is equal to 108684. And if the integer limit of PHP is 2,147,483,647 then there's plenty of room to expand (108684 is smaller than 2,147,483,647 if you missed the point :) )
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

timvw wrote:(you are mistaken) If one bit is the sign then you end up with -0 and +0
No, you are mistaken, what (I think) you are precieving to be a bit representation of -0 is (in the case of a 32bit signed integer) a bit representation of -2,147,483,648.
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

redmonkey wrote:No, you are mistaken, what (I think) you are precieving to be a bit representation of -0 is (in the case of a 32bit signed integer) a bit representation of -2,147,483,648.
It is logical. Either way there's still much room to add another permission or two, which is great :)
Post Reply