You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cassandra.apache.org by john xie <sh...@gmail.com> on 2010/07/07 13:08:40 UTC

Fwd: a_long_is_exactly_8_bytes

http://wiki.apache.org/cassandra/FAQ#a_long_is_exactly_8_bytes

        /**
         * Takes php integer and packs it to 64bit (8 bytes) long big
endian binary representation.
         * @param  $x integer
         * @return string eight bytes long binary repersentation of
the integer in big endian order.
         */
        public static function pack_longtype($x) {
                return pack('C8', ($x >> 56) & 0xff, ($x >> 48) &
0xff, ($x >> 40) & 0xff, ($x >> 32) & 0xff,
                                ($x >> 24) & 0xff, ($x >> 16) & 0xff,
($x >> 8) & 0xff, $x & 0xff);
        }

        /**
         * Takes eight bytes long big endian binary representation of
an integer and unpacks it to a php integer.
         * @param  $x
         * @return php integer
         */
        public static function unpack_longtype($x) {
                $a = unpack('C8', $x);
                return ($a[1] << 56) + ($a[2] << 48) + ($a[3] << 40) +
($a[4] << 32) + ($a[5] << 24) + ($a[6] << 16) + ($a[7] << 8) + $a[8];
        }

 test.php:

 $input=1;

 $output=unpack_longtype(pack_longtype($input));

  print "input=".$input." but output = ".$output."\n";

 $input=2;

 $output=unpack_longtype(pack_longtype($input));

 print "input=".$input." but output = ".$output."\n";

  $input=3;

 $output=unpack_longtype(pack_longtype($input));

 print "input=".$input." but output = ".$output."\n";



but in log file:

input=1 but output = 2

input=2 but output = 4

input=3 but output = 6

Re: a_long_is_exactly_8_bytes

Posted by john xie <sh...@gmail.com>.
thanks

yes, it works on x86_64

my environment:
Darwin  10.4.0 Darwin Kernel Version 10.4.0: Fri Apr 23 18:28:53 PDT 2010;
root:xnu-1504.7.4~1/RELEASE_I386 i386

is have any Compatible solutions  on 64bit and 32bit?


2010/7/7 Juho Mäkinen <ju...@gmail.com>

> Hello.
>
> I added that code and it works on our x86_64 bit Intel machine (just
> tested with your test.php). What environment are you using? I haven't
> tested the code on a 32bit machine and I believe that it will not work
> there. I should have propably added a note to the wiki that it wont
> work on 32bit environment.
>
>  - Juho Mäkinen
>
> On Wed, Jul 7, 2010 at 2:08 PM, john xie <sh...@gmail.com> wrote:
> >
> >
> > http://wiki.apache.org/cassandra/FAQ#a_long_is_exactly_8_bytes
> >
> >         /**
> >          * Takes php integer and packs it to 64bit (8 bytes) long big
> endian
> > binary representation.
> >          * @param  $x integer
> >          * @return string eight bytes long binary repersentation of the
> > integer in big endian order.
> >          */
> >         public static function pack_longtype($x) {
> >                 return pack('C8', ($x >> 56) & 0xff, ($x >> 48) & 0xff,
> ($x
> >>> 40) & 0xff, ($x >> 32) & 0xff,
> >                                 ($x >> 24) & 0xff, ($x >> 16) & 0xff, ($x
> >>
> > 8) & 0xff, $x & 0xff);
> >         }
> >
> >         /**
> >          * Takes eight bytes long big endian binary representation of an
> > integer and unpacks it to a php integer.
> >          * @param  $x
> >          * @return php integer
> >          */
> >         public static function unpack_longtype($x) {
> >                 $a = unpack('C8', $x);
> >                 return ($a[1] << 56) + ($a[2] << 48) + ($a[3] << 40) +
> > ($a[4] << 32) + ($a[5] << 24) + ($a[6] << 16) + ($a[7] << 8) + $a[8];
> >         }
> >
> > test.php:
> >
> >  $input=1;
> >
> >  $output=unpack_longtype(pack_longtype($input));
> >
> >   print "input=".$input." but output = ".$output."\n";
> >
> >  $input=2;
> >
> >  $output=unpack_longtype(pack_longtype($input));
> >
> >  print "input=".$input." but output = ".$output."\n";
> >
> >   $input=3;
> >
> >  $output=unpack_longtype(pack_longtype($input));
> >
> >  print "input=".$input." but output = ".$output."\n";
> >
> > but in log file:
> >
> > input=1 but output = 2
> >
> > input=2 but output = 4
> >
> > input=3 but output = 6
> >
> >
> >
>



-- 
谢善峰    johnxie
联系方式:
MSN:johnxiegood@hotmail.com <MS...@hotmail.com>
GTALK: shanfenggood@gmail.com
E_mail:johnxie@topgene.net <E_...@topgene.net>
------------------------------------------
智信环宇  Internet Service On_Demand

Re: a_long_is_exactly_8_bytes

Posted by Juho Mäkinen <ju...@gmail.com>.
Hello.

I added that code and it works on our x86_64 bit Intel machine (just
tested with your test.php). What environment are you using? I haven't
tested the code on a 32bit machine and I believe that it will not work
there. I should have propably added a note to the wiki that it wont
work on 32bit environment.

 - Juho Mäkinen

On Wed, Jul 7, 2010 at 2:08 PM, john xie <sh...@gmail.com> wrote:
>
>
> http://wiki.apache.org/cassandra/FAQ#a_long_is_exactly_8_bytes
>
>         /**
>          * Takes php integer and packs it to 64bit (8 bytes) long big endian
> binary representation.
>          * @param  $x integer
>          * @return string eight bytes long binary repersentation of the
> integer in big endian order.
>          */
>         public static function pack_longtype($x) {
>                 return pack('C8', ($x >> 56) & 0xff, ($x >> 48) & 0xff, ($x
>>> 40) & 0xff, ($x >> 32) & 0xff,
>                                 ($x >> 24) & 0xff, ($x >> 16) & 0xff, ($x >>
> 8) & 0xff, $x & 0xff);
>         }
>
>         /**
>          * Takes eight bytes long big endian binary representation of an
> integer and unpacks it to a php integer.
>          * @param  $x
>          * @return php integer
>          */
>         public static function unpack_longtype($x) {
>                 $a = unpack('C8', $x);
>                 return ($a[1] << 56) + ($a[2] << 48) + ($a[3] << 40) +
> ($a[4] << 32) + ($a[5] << 24) + ($a[6] << 16) + ($a[7] << 8) + $a[8];
>         }
>
> test.php:
>
>  $input=1;
>
>  $output=unpack_longtype(pack_longtype($input));
>
>   print "input=".$input." but output = ".$output."\n";
>
>  $input=2;
>
>  $output=unpack_longtype(pack_longtype($input));
>
>  print "input=".$input." but output = ".$output."\n";
>
>   $input=3;
>
>  $output=unpack_longtype(pack_longtype($input));
>
>  print "input=".$input." but output = ".$output."\n";
>
> but in log file:
>
> input=1 but output = 2
>
> input=2 but output = 4
>
> input=3 but output = 6
>
>
>