[PHP] Re: Can this code go faster?

2005-04-29 Thread Rolf Østvik
Just a couple of comments about those 0x.. and 11
in my post.
(By the way, i haven't looked to closely on speed even if the OP asked
for that, i was unsure on how he was coding his numbers). 

[EMAIL PROTECTED] (Rolf Østvik) wrote in
news:[EMAIL PROTECTED]: 

 [EMAIL PROTECTED] (René Fournier) wrote in 
 news:[EMAIL PROTECTED]:
 
 I need to convert a binary number of arbitrary length to a signed 
 integer.
 
 If you on the other hand know that the string should represent an 8
 bit in two's complement then this could work:
 
 function bin2int ($bin) {
 $a =  bindec($bin);
 
 $val = ($a | 0x80) ? (0xff00 | $a) : $a;
if the input is a 8 bit signed binary string then bit 7 determine the
sign. 0xff00 will then make this to an 32 signed integer

 
 return $val;
 }
 
 function bin2int ($bin) {
 if (substr($bin,0,1) == 1) {
 // NEGATIVE
 $val = bindec(substr(
   .$bin,-32)); 
bindec returns a 32 bit signed integer, i just make sure that the
leftmost bits in the 32 character long string are 1 if the leftmost bit
in the original string is 1. 
 } else {
 // POSITIVE
 $val = bindec($bin); 
 }
 return $val;
 }
 

-- 
Rolf

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



[PHP] Re: Can this code go faster?

2005-04-26 Thread Rolf Østvik
[EMAIL PROTECTED] (René Fournier) wrote in 
news:[EMAIL PROTECTED]:

 I need to convert a binary number of arbitrary length to a signed 
 integer.
 This is how I'm doing it now:
 CODE
 
 function bin2int ($bin) {
  if (substr($bin,0,1) == 1) {
   $val = 0 - bindec(substr($bin,1)); // NEGATIVE
   } else {
   $val = bindec(substr($bin,1)); // POSITIVE
   }
  }
 
 echo bin2int(1101).'br /';
 echo bin2int(10001101);
 
 OUTPUT
 
 13
 -13
 
 As you can see, if the most-significant bit is 1, then the rest of the 
 value is negative. If the first bit is 0, then the rest is positive.

If this is what you want then your numeric representation of negative 
numbers is not standard. In that case your function is as good as you can 
get.

This is the standard two's complement representation:
1101  is 13
0010  is -13
10001101  is -115

You specify the most-significant bit as sign bit, and that you can have a 
arbitrary lenght string representing a binary number.
Then 010 will be different than 10, is that correct?


If you on the other hand know that the string should represent an 8 bit in 
two's complement then this could work:

function bin2int ($bin) {
$a =  bindec($bin);

$val = ($a | 0x80) ? (0xff00 | $a) : $a;

return $val;
}

//  or by using if instead of ? and :
//function bin2int ($bin) {
//$a =  bindec($bin);
//  if ( $a | 0x80) {
//  $val = (0xff00 | $a);
//  } else {
//  $val =$a;
//  }
//
//return $val;
//}

If the string represent a 16 bit number use
$val = ($ | 0x8000) ? (0x | $a) : $a;

If the string represent a 32 bit number use only bindec

If you still want
 - a leading 1 to represent a negative number, 
 - and that the string can have arbitrary lenght, 
 - but you want correct two's complement representation
then we need something different, e.g:

function bin2int ($bin) {
if (substr($bin,0,1) == 1) {
// NEGATIVE
$val = bindec(substr(.$bin,-32)); 
} else {
   // POSITIVE
$val = bindec($bin); 
}
return $val;
}

-- 
Rolf

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



[PHP] Re: collapse SELECT from multiple tables

2005-03-29 Thread Rolf Østvik
[EMAIL PROTECTED] (David Christensen) wrote in
news:[EMAIL PROTECTED]: 

 I'm trying to figure out the best way to handle a SELECT from multiple
 tables where a single ID in tableA relates to multiple ID's in tableB:
 
 SELECT tableA.ID, tableB.data FROM tableA, tableB WHERE tableA.ID=3
 AND tableA.ID=tableB.tableAID
 
 What I'm trying to product is an array output similar to:
 
| ID | data |
| 3  | data1, data2, data3 |
 
 if the table data is represented by this:
 
 tableA
| ID | something  |
| 1  | something1 |
| 2  | something2 |
| 3  | something3 |
 
 tableB
| ID | tableAID | data  |
| 1  | 3| data1 |
| 2  | 3| data2 |
| 3  | 3| data2 |

Suggestion 1 and 2 may depend of which database you use.
Suggestion 3 have been touched in other answers.

Suggestion 1:
Create a custom function which takes the tableA.ID as parameter and run
a query on tableB and creates an string as answer. I do this in an
application i use PostgreSQL. 

Suggestion 2:
Create an aggregate. I havent yet tried this in PostgreSQL.

Suggestion 3:
Order your sql result and loop over the data.

-- 
Rolf Østvik

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



Re: [PHP] Re: [NEWBIE GUIDE] For the benefit of new members

2005-01-26 Thread Rolf Østvik
[EMAIL PROTECTED] (John Nichel) wrote in
news:[EMAIL PROTECTED]: 

 Jay Blanchard wrote:
 [snip]
 CR Just a thought, but would it be worth someone posting the list
 CR once a week to catch new users as they sign up?
 
 Isn't it posted once a month as it is?
 [/snip]
 
 It used to be, but it seems that it hasn't been posted in a while. So
 I retrieved it and posted it. I was thinking about setting up a cron
 to post it every other day or so.
 
 Didn't it used to get sent out to people when they subscribed to the 
 list too?  Anyone know if that still happens?

Well, i only accesses this list on usenet. I haven't subscribed to
anything. 

-- 
Rolf

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