Page 1 of 1
Bitwise Operators
Posted: Tue Dec 03, 2002 8:58 pm
by AƩrolithe
I am reading Programming PHP and it has a section on bitwise operators. They seemed rather useless to me until I thought of encryption, and this broguht up two questions I was hoping I could find the answer to here:
1) Are bitwise operators used for encryption/other security meathods?
and
2) Does it serve any other purpouse?
Posted: Tue Dec 03, 2002 10:43 pm
by mydimension
yep, they are. bitwise operators come in very handy for encryption cause one in particular is its own inverse function. the logical OR or XOR. i wrote a visual basic program that generated an encryption stream and byte by byte enecrypted the file using XOR.
a logical OR works like this: one OR the other BUT NOT both.
so if you have two binary strings XOR'd together, this would be the result:
Code: Select all
1st 2nd out
01100100 XOR 01001101 = 00101001
now, lets reverse it:
Code: Select all
out 2nd 1st
00101001 XOR 01001101 = 01100100
anyway, the way i used it was on each indiviual character of the file, like so:
chrOutput = chrInput XOR chrEncrypt
for use in PHP, use the circumflux (^) to do bitwise operation. hope this has been useful to you.
Posted: Wed Dec 04, 2002 4:24 am
by volka
I hope you do not use this on vital data or your second pattern is truely random and used only once
There are plenty of purposes for bitwise operations.
Code: Select all
// swapping to numerical values
$tmp = $a;
$a = $b;
$b = $tmp;
can be done by
ok, not very useful in php but e.g. in C it spares a third (temp.) variable
say you want to store multiple selections made by users in a single databasefield. You might take the values and concatenate them (or use implode() or what ever) and store it in a text-type field. Compared to numerical values strings are slooooow. But you might create a single numerical value with bitwise operations. e.g. having
Code: Select all
<form ....>
<input type="checkbox" name="chkї]" value="1" />...
<input type="checkbox" name="chkї]" value="2" />...
<input type="checkbox" name="chkї]" value="4" /> test
<input type="checkbox" name="chkї]" value="8" />...
...
</form>
Code: Select all
$nSelections = 0;
foreach($_POST['chk'] as $value)
$nSelections |= $value;
to see wether
test has been selected e.g.
- you will encounter bitwise operations in APIs as well
- http://www.php.net/manual/en/function.preg-split.php
array preg_split ( string pattern, string subject [, int limit [, int flags]])
...
flags can be any combination of the following flags (combined with bitwise | operator):
- Windows Platform SDK: Storage - GetFileAttributes
DWORD GetFileAttributes(
LPCTSTR lpFileName
);
...
The attributes can be one or more of the following values.
[long list below here

]
to check for directory flag you may performCode: Select all
if ( (GetFileAttributes("someFilename") & FILE_ATTRIBUTE_DIRECTORY) != 0) ....
The implementation of the beloved md5() function also makes intense use of bitwise operations
Code: Select all
PHPAPI void PHP_MD5Update(PHP_MD5_CTX * context, const unsigned char *input,
unsigned int inputLen)
{
unsigned int i, index, partLen;
/* Compute number of bytes mod 64 */
index = (unsigned int) ((context->countї0] >> 3) & 0x3F);
/* Update number of bits */
if ((context->countї0] += ((php_uint32) inputLen << 3))
< ((php_uint32) inputLen << 3))
context->countї1]++;
context->countї1] += ((php_uint32) inputLen >> 29);
...
and so on and so on

Posted: Wed Dec 04, 2002 6:20 pm
by AƩrolithe
Thanks for the help.