Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:73681 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 58380 invoked from network); 13 Apr 2014 12:21:00 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Apr 2014 12:21:00 -0000 Authentication-Results: pb1.pair.com smtp.mail=php@marc-bennewitz.de; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=php@marc-bennewitz.de; sender-id=unknown Received-SPF: error (pb1.pair.com: domain marc-bennewitz.de from 80.237.132.171 cause and error) X-PHP-List-Original-Sender: php@marc-bennewitz.de X-Host-Fingerprint: 80.237.132.171 wp164.webpack.hosteurope.de Received: from [80.237.132.171] ([80.237.132.171:41322] helo=wp164.webpack.hosteurope.de) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5C/81-49914-A218A435 for ; Sun, 13 Apr 2014 08:20:59 -0400 Received: from dslb-092-078-051-105.pools.arcor-ip.net ([92.78.51.105] helo=[192.168.178.27]); authenticated by wp164.webpack.hosteurope.de running ExIM with esmtpsa (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) id 1WZJP6-0002LT-2g; Sun, 13 Apr 2014 14:20:56 +0200 Message-ID: <534A8121.6090205@marc-bennewitz.de> Date: Sun, 13 Apr 2014 14:20:49 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 MIME-Version: 1.0 To: PHP Internals Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-bounce-key: webpack.hosteurope.de;php@marc-bennewitz.de;1397391659;237f9538; Subject: Shifting bits of a binary string From: php@marc-bennewitz.de (Marc Bennewitz) Hi List, I hope I'm on the right list but I can't find any other helpful. I have a binary string and I would like to work with bitwise operators. The only help I found was to convert it to an integer. That's ok but it results in some questions: - What if the binary data is more than 32/64 bits long? - Why converting binary data of form one into binary data of another form only to manipulate bits? So I simply tested what's going on if I operate on a string directly but on shifting I get the same wrong result every time. (Testscript below) On reading the manual the only note for strings are the following: (http://www.php.net/manual/en/language.operators.bitwise.php) > Be aware of data type conversions. If both the left-hand and right-hand parameters are strings, the bitwise operator will operate on the characters' ASCII values. Why such bit operators doesn't work with strings? Why there is not helpful information about in the manual. Why on operation something not working doesn't result in an error/notice but in a completely unexpected value? Greetings Marc Shift to the left: var_dump(decbin(ord(chr(1)))); for ($i=0; $i<10; $i++) { var_dump(decbin(ord(chr(1) << $i))); } Output: string(1) "1" string(6) "110000" string(6) "110000" string(6) "110000" string(6) "110000" string(6) "110000" string(6) "110000" string(6) "110000" string(6) "110000" string(6) "110000" string(6) "110000" Shift to the right: var_dump(decbin(ord(chr(32)))); for ($i=0; $i<10; $i++) { var_dump(decbin(ord(chr(32) >> $i))); } Output: string(1) "100000" string(6) "110000" string(6) "110000" string(6) "110000" string(6) "110000" string(6) "110000" string(6) "110000" string(6) "110000" string(6) "110000" string(6) "110000" string(6) "110000"