incubator-cassandra-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From john xie <shanfengg...@gmail.com>
Subject Fwd: a_long_is_exactly_8_bytes
Date Wed, 07 Jul 2010 11:08:40 GMT
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

Mime
View raw message