Re: [PHP] Bitwise operators and check if an bit is NOT within the flag.

2006-07-07 Thread Richard Lynch
On Tue, July 4, 2006 7:35 am, Mathijs wrote:
 //Do if VALIDATE_CHECK1 is set BUT NOT when VALIDATE_CHECK3 is set.
 if ($flag2  self::VALIDATE_CHECK1  $flag2  ~self::VALIDATE_CHECK3)

Did you check operator precedence for  versus ?

Perhaps you just need parentheses...

I'm also not at all sure the ~self::VALIDATE_CHECK3 is doing what you
want...  Echo that out and see if it's the number you would expect...

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Bitwise operators and check if an bit is NOT within the flag.

2006-07-04 Thread Jochem Maas
Mathijs wrote:
 Hello there.
 
 I am working with some bitwise Operators for validating some variables.
 Now i need to know if an certain bit is NOT set and an other bit IS set.
 
 Example.
 
 ?php
 
 const VALIDATE_CHECK1 = 1;
 const VALIDATE_CHECK2 = 2;
 const VALIDATE_CHECK3 = 4;
 const VALIDATE_ALL= 7;
 
 //--Example 1 - This works nice.
 $flag1 = self::VALIDATE_CHECK1;
 
 //Do if VALIDATE_CHECK1 is set
 if ($flag1  self::VALIDATE_CHECK1) {
 print 'Validate 1';
 }
 //Do if VALIDATE_CHECK2 is set
 if ($flag1  self::VALIDATE_CHECK2) {
 print 'Validate 2';
 }
 
 //--Example 2 - I want to check if VALIDATE_CHECK3 is not set and then
 continue.
 $flag2 = self::VALIDATE_ALL;
 
 //Do if VALIDATE_CHECK1 is set BUT NOT when VALIDATE_CHECK3 is set.
 if ($flag2  self::VALIDATE_CHECK1  $flag2  ~self::VALIDATE_CHECK3) {

class Test {
const VALIDATE_CHECK1 = 1;
const VALIDATE_CHECK2 = 2;
const VALIDATE_CHECK3 = 4;
const VALIDATE_ALL= 7;
static function check($flag2)
{
if (($flag2  self::VALIDATE_CHECK1) 
!($flag2  self::VALIDATE_CHECK3)) print Only Validate 1;
}
}

echo First attempt: ;
Test::check( Test::VALIDATE_ALL );
echo \nSecond attempt: ;
Test::check( Test::VALIDATE_CHECK1 );
echo \n;
 print 'Only Validate 1';
 }
 //etc...
 ?
 
 This last example i can't seem to get to work.
 I Want to only do that when for example VALIDATE_CHECK3 is not within
 the $flag2.
 How can i do this?
 
 Thx in advanced.
 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Bitwise operators and check if an bit is NOT within theflag.

2006-07-04 Thread Mathijs

Jochem Maas wrote:

Mathijs wrote:

Hello there.

I am working with some bitwise Operators for validating some variables.
Now i need to know if an certain bit is NOT set and an other bit IS set.

Example.

?php

const VALIDATE_CHECK1 = 1;
const VALIDATE_CHECK2 = 2;
const VALIDATE_CHECK3 = 4;
const VALIDATE_ALL= 7;

//--Example 1 - This works nice.
$flag1 = self::VALIDATE_CHECK1;

//Do if VALIDATE_CHECK1 is set
if ($flag1  self::VALIDATE_CHECK1) {
print 'Validate 1';
}
//Do if VALIDATE_CHECK2 is set
if ($flag1  self::VALIDATE_CHECK2) {
print 'Validate 2';
}

//--Example 2 - I want to check if VALIDATE_CHECK3 is not set and then
continue.
$flag2 = self::VALIDATE_ALL;

//Do if VALIDATE_CHECK1 is set BUT NOT when VALIDATE_CHECK3 is set.
if ($flag2  self::VALIDATE_CHECK1  $flag2  ~self::VALIDATE_CHECK3) {


class Test {
const VALIDATE_CHECK1 = 1;
const VALIDATE_CHECK2 = 2;
const VALIDATE_CHECK3 = 4;
const VALIDATE_ALL= 7;
static function check($flag2)
{
if (($flag2  self::VALIDATE_CHECK1) 
!($flag2  self::VALIDATE_CHECK3)) print Only Validate 1;
}
}

echo First attempt: ;
Test::check( Test::VALIDATE_ALL );
echo \nSecond attempt: ;
Test::check( Test::VALIDATE_CHECK1 );
echo \n;

print 'Only Validate 1';
}
//etc...
?

This last example i can't seem to get to work.
I Want to only do that when for example VALIDATE_CHECK3 is not within
the $flag2.
How can i do this?

Thx in advanced.




Thank you very much.
This seems to work :).

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Bitwise operators and check if an bit is NOT within theflag.

2006-07-04 Thread Jochem Maas
Mathijs wrote:
 Jochem Maas wrote:
 Mathijs wrote:


...


 
 
 Thank you very much.
 This seems to work :).

cool. heres's a couple of funcs that might help you to understand bitwise
operations better:

?php

/* whether there is only 1 single bit set or not */
function single_bit_set(/*int*/ $i)
{
// beware: if $i is zero !($i  ~get_ls1bit($i)) returns true;
return $i ? !($i  ~get_ls1bit($i)): false;
}

/* return the value of the least significant bit */
function get_ls1bit(/*int*/ $i)
{
for ($j = 1; $i  !($i  $j); $j = 1);
return $i ? $j : 0;
}

/* return the value of the most significant bit */
function get_ms1bit(/*int*/ $i)
{
$x = 0;
for ($j = $i; $i  !($j == 1); $j = 1) { $x++; }
return $i ? $j = $x: 0;
}

/* doesn't break when exponents are near the wordsize
 * of the machine as the native xor does, here is some example code to
 * illustrate:

php -r '
// include function definition here
$x = 3851235679;
$y = 43814;
echo \nThis is the value we want;
echo \n3851262585;

echo \nThe result of a native xor operation on integer values is treated 
as a signed integer;
echo \n.($x ^ $y);

echo \nWe therefore perform the MSB separately;
echo \n.get_xor32($x, $y).\n;
'

 */
function get_xor32(/*int*/ $a, /*int*/ $b)
{
   $a1 = $a  0x7FFF;
   $a2 = $a  0x;
   $a3 = $a  0x8000;
   $b1 = $b  0x7FFF;
   $b2 = $b  0x;
   $b3 = $b  0x8000;

   $c = ($a3 != $b3) ? 0x8000 : 0;

   return (($a1 ^ $b1) |($a2 ^ $b2)) + $c;
}

function get_bit_str($var, $safety = 0)
{
$rtn = '';

$var = intval($var);
if ($var  0) { $var = 0 - $var; }

while ($var != 0 /* $safety  31*/) {
$rtn .= ($var  1);
$var = 1;
$safety++;
}

return $rtn;
}

 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Bitwise operators and check if an bit is NOT within theflag.

2006-07-04 Thread Mathijs

Jochem Maas wrote:

Mathijs wrote:

Jochem Maas wrote:

Mathijs wrote:



...



Thank you very much.
This seems to work :).


cool. heres's a couple of funcs that might help you to understand bitwise
operations better:

?php

/* whether there is only 1 single bit set or not */
function single_bit_set(/*int*/ $i)
{
// beware: if $i is zero !($i  ~get_ls1bit($i)) returns true;
return $i ? !($i  ~get_ls1bit($i)): false;
}

/* return the value of the least significant bit */
function get_ls1bit(/*int*/ $i)
{
for ($j = 1; $i  !($i  $j); $j = 1);
return $i ? $j : 0;
}

/* return the value of the most significant bit */
function get_ms1bit(/*int*/ $i)
{
$x = 0;
for ($j = $i; $i  !($j == 1); $j = 1) { $x++; }
return $i ? $j = $x: 0;
}

/* doesn't break when exponents are near the wordsize
 * of the machine as the native xor does, here is some example code to
 * illustrate:

php -r '
// include function definition here
$x = 3851235679;
$y = 43814;
echo \nThis is the value we want;
echo \n3851262585;

echo \nThe result of a native xor operation on integer values is treated as a 
signed integer;
echo \n.($x ^ $y);

echo \nWe therefore perform the MSB separately;
echo \n.get_xor32($x, $y).\n;
'

 */
function get_xor32(/*int*/ $a, /*int*/ $b)
{
   $a1 = $a  0x7FFF;
   $a2 = $a  0x;
   $a3 = $a  0x8000;
   $b1 = $b  0x7FFF;
   $b2 = $b  0x;
   $b3 = $b  0x8000;

   $c = ($a3 != $b3) ? 0x8000 : 0;

   return (($a1 ^ $b1) |($a2 ^ $b2)) + $c;
}

function get_bit_str($var, $safety = 0)
{
$rtn = '';

$var = intval($var);
if ($var  0) { $var = 0 - $var; }

while ($var != 0 /* $safety  31*/) {
$rtn .= ($var  1);
$var = 1;
$safety++;
}

return $rtn;
}


Thx alot :).

It will sure help me understanding it better :).

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Bitwise operators and check if an bit is NOT within theflag.

2006-07-04 Thread Mathijs

Jochem Maas wrote:

Mathijs wrote:

Jochem Maas wrote:

Mathijs wrote:



...



Thank you very much.
This seems to work :).


cool. heres's a couple of funcs that might help you to understand bitwise
operations better:

?php

/* whether there is only 1 single bit set or not */
function single_bit_set(/*int*/ $i)
{
// beware: if $i is zero !($i  ~get_ls1bit($i)) returns true;
return $i ? !($i  ~get_ls1bit($i)): false;
}

/* return the value of the least significant bit */
function get_ls1bit(/*int*/ $i)
{
for ($j = 1; $i  !($i  $j); $j = 1);
return $i ? $j : 0;
}

/* return the value of the most significant bit */
function get_ms1bit(/*int*/ $i)
{
$x = 0;
for ($j = $i; $i  !($j == 1); $j = 1) { $x++; }
return $i ? $j = $x: 0;
}

/* doesn't break when exponents are near the wordsize
 * of the machine as the native xor does, here is some example code to
 * illustrate:

php -r '
// include function definition here
$x = 3851235679;
$y = 43814;
echo \nThis is the value we want;
echo \n3851262585;

echo \nThe result of a native xor operation on integer values is treated as a 
signed integer;
echo \n.($x ^ $y);

echo \nWe therefore perform the MSB separately;
echo \n.get_xor32($x, $y).\n;
'

 */
function get_xor32(/*int*/ $a, /*int*/ $b)
{
   $a1 = $a  0x7FFF;
   $a2 = $a  0x;
   $a3 = $a  0x8000;
   $b1 = $b  0x7FFF;
   $b2 = $b  0x;
   $b3 = $b  0x8000;

   $c = ($a3 != $b3) ? 0x8000 : 0;

   return (($a1 ^ $b1) |($a2 ^ $b2)) + $c;
}

function get_bit_str($var, $safety = 0)
{
$rtn = '';

$var = intval($var);
if ($var  0) { $var = 0 - $var; }

while ($var != 0 /* $safety  31*/) {
$rtn .= ($var  1);
$var = 1;
$safety++;
}

return $rtn;
}


Thx alot :).

It will sure help me understanding it better :).

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Bitwise operators and check if an bit is NOT within theflag.

2006-07-04 Thread Mathijs

Jochem Maas wrote:

Mathijs wrote:

Jochem Maas wrote:

Mathijs wrote:



...



Thank you very much.
This seems to work :).


cool. heres's a couple of funcs that might help you to understand bitwise
operations better:

?php

/* whether there is only 1 single bit set or not */
function single_bit_set(/*int*/ $i)
{
// beware: if $i is zero !($i  ~get_ls1bit($i)) returns true;
return $i ? !($i  ~get_ls1bit($i)): false;
}

/* return the value of the least significant bit */
function get_ls1bit(/*int*/ $i)
{
for ($j = 1; $i  !($i  $j); $j = 1);
return $i ? $j : 0;
}

/* return the value of the most significant bit */
function get_ms1bit(/*int*/ $i)
{
$x = 0;
for ($j = $i; $i  !($j == 1); $j = 1) { $x++; }
return $i ? $j = $x: 0;
}

/* doesn't break when exponents are near the wordsize
 * of the machine as the native xor does, here is some example code to
 * illustrate:

php -r '
// include function definition here
$x = 3851235679;
$y = 43814;
echo \nThis is the value we want;
echo \n3851262585;

echo \nThe result of a native xor operation on integer values is treated as a 
signed integer;
echo \n.($x ^ $y);

echo \nWe therefore perform the MSB separately;
echo \n.get_xor32($x, $y).\n;
'

 */
function get_xor32(/*int*/ $a, /*int*/ $b)
{
   $a1 = $a  0x7FFF;
   $a2 = $a  0x;
   $a3 = $a  0x8000;
   $b1 = $b  0x7FFF;
   $b2 = $b  0x;
   $b3 = $b  0x8000;

   $c = ($a3 != $b3) ? 0x8000 : 0;

   return (($a1 ^ $b1) |($a2 ^ $b2)) + $c;
}

function get_bit_str($var, $safety = 0)
{
$rtn = '';

$var = intval($var);
if ($var  0) { $var = 0 - $var; }

while ($var != 0 /* $safety  31*/) {
$rtn .= ($var  1);
$var = 1;
$safety++;
}

return $rtn;
}


Thx alot :).

It will sure help me understanding it better :).

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Bitwise operators and check if an bit is NOT within theflag.

2006-07-04 Thread Mathijs

Jochem Maas wrote:

Mathijs wrote:

Jochem Maas wrote:

Mathijs wrote:



...



Thank you very much.
This seems to work :).


cool. heres's a couple of funcs that might help you to understand bitwise
operations better:

?php

/* whether there is only 1 single bit set or not */
function single_bit_set(/*int*/ $i)
{
// beware: if $i is zero !($i  ~get_ls1bit($i)) returns true;
return $i ? !($i  ~get_ls1bit($i)): false;
}

/* return the value of the least significant bit */
function get_ls1bit(/*int*/ $i)
{
for ($j = 1; $i  !($i  $j); $j = 1);
return $i ? $j : 0;
}

/* return the value of the most significant bit */
function get_ms1bit(/*int*/ $i)
{
$x = 0;
for ($j = $i; $i  !($j == 1); $j = 1) { $x++; }
return $i ? $j = $x: 0;
}

/* doesn't break when exponents are near the wordsize
 * of the machine as the native xor does, here is some example code to
 * illustrate:

php -r '
// include function definition here
$x = 3851235679;
$y = 43814;
echo \nThis is the value we want;
echo \n3851262585;

echo \nThe result of a native xor operation on integer values is treated as a 
signed integer;
echo \n.($x ^ $y);

echo \nWe therefore perform the MSB separately;
echo \n.get_xor32($x, $y).\n;
'

 */
function get_xor32(/*int*/ $a, /*int*/ $b)
{
   $a1 = $a  0x7FFF;
   $a2 = $a  0x;
   $a3 = $a  0x8000;
   $b1 = $b  0x7FFF;
   $b2 = $b  0x;
   $b3 = $b  0x8000;

   $c = ($a3 != $b3) ? 0x8000 : 0;

   return (($a1 ^ $b1) |($a2 ^ $b2)) + $c;
}

function get_bit_str($var, $safety = 0)
{
$rtn = '';

$var = intval($var);
if ($var  0) { $var = 0 - $var; }

while ($var != 0 /* $safety  31*/) {
$rtn .= ($var  1);
$var = 1;
$safety++;
}

return $rtn;
}


Thx alot :).

It will sure help me understanding it better :).

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Bitwise operators

2005-09-26 Thread Chris W. Parker
[EMAIL PROTECTED] mailto:[EMAIL PROTECTED]
on Monday, September 26, 2005 9:18 AM said:

 So i ask what this output?
 
 $a = 4;
 $b = 3;
 
 echo  $a  $b;
 echo  $a  $b;

You just spent 3-5 minutes writing an email and now almost 10 minutes
waiting for a reply to something that would have taken you 2 minutes to
test on your own.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Bitwise operators

2005-09-26 Thread Robin Vickery
On 9/26/05, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
 From php manual:

 $a  $b Shift leftShift the bits of $a $b steps to the left (each step
 means multiply by two)
 $a  $b Shift rightShift the bits of $a $b steps to the right (each step
 means divide by two)


 So i ask what this output?

 $a = 4;
 $b = 3;

 echo  $a  $b;

32

 echo  $a  $b;

0

So your program will output '320'.

What was the problem again?

  -robin

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Bitwise operators

2005-09-26 Thread cron
I tested; I don't want to waste peoples time. Rewriting the question:

 

$a = 4;
$b = 3;

 

$c =   $a  $b;
$d =  $a  $b;

 

echo c =  . $c . br;
echo d =  . $d . br;

 

this outputs:

 

c = 32
d = 0

 

The question is why?

 

Angelo



- Original Message - 
From: Chris W. Parker [EMAIL PROTECTED]
To: php-general@lists.php.net
Sent: Monday, September 26, 2005 1:28 PM
Subject: RE: [PHP] Bitwise operators


[EMAIL PROTECTED] mailto:[EMAIL PROTECTED]
on Monday, September 26, 2005 9:18 AM said:

 So i ask what this output?
 
 $a = 4;
 $b = 3;
 
 echo  $a  $b;
 echo  $a  $b;

You just spent 3-5 minutes writing an email and now almost 10 minutes
waiting for a reply to something that would have taken you 2 minutes to
test on your own.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Bitwise operators

2005-09-26 Thread Chris Boget

I tested; I don't want to waste peoples time. Rewriting the question:
this outputs:
c = 32
d = 0
The question is why?


First row is the bit's number and the second row is the bit's value:

#8  | #7  | #6 | #5 | #4 | #3 | #2 | #1
---
128 | 64  | 32 | 16 |  8 |  4 |  2 |  1

So your variable $a, which has a value of 4 starts out in the bit position
of #3 with that bit turned on..  Shift it left 3 (value of $b) spaces and 
you

end up with bit #6 getting turned on giving you a value of 32, which is what
$c is echoing out.

Shifting to the right 3 spaces from the same starting position nets you 0 
with

all the bits turned off.  At least, I'm pretty sure that's right.

thnx,
Chris 


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Bitwise operators

2005-09-26 Thread cron
The second value is the number of spaces to shift, dint realize that.



Thanks for your time Chris.



Angelo



- Original Message - 
From: Chris Boget [EMAIL PROTECTED]
To: [EMAIL PROTECTED]; php-general@lists.php.net
Sent: Monday, September 26, 2005 2:02 PM
Subject: Re: [PHP] Bitwise operators


 I tested; I don't want to waste peoples time. Rewriting the question:
  this outputs:
  c = 32
  d = 0
  The question is why?

 First row is the bit's number and the second row is the bit's value:

 #8  | #7  | #6 | #5 | #4 | #3 | #2 | #1
 ---
 128 | 64  | 32 | 16 |  8 |  4 |  2 |  1

 So your variable $a, which has a value of 4 starts out in the bit position
 of #3 with that bit turned on..  Shift it left 3 (value of $b) spaces and
 you
 end up with bit #6 getting turned on giving you a value of 32, which is
what
 $c is echoing out.

 Shifting to the right 3 spaces from the same starting position nets you 0
 with
 all the bits turned off.  At least, I'm pretty sure that's right.

 thnx,
 Chris

 -- 
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php