Using int type to implement right shift operation of type unsinged int_PHP tutorial

WBOY
Release: 2016-07-21 14:55:33
Original
1103 people have browsed it

There are many scripting languages ​​that do not have unsinged int, such as PHP. Generally speaking, there is no difference between bit operations between int and unsinged int. Except for the right shift operation, int is filled with 1 and unsinged int is filled with 0. I thought about it for a long time and finally thought of a better method. However, this method , the number of right shifts is at least greater than 1. Generally speaking, shifting right by 0 has little meaning.

(($a >> 1) & 0x7fffffff) >> ($n -1) Here $n is the number of right shifts. $n >= 1

For example, to implement md5 encryption, generally the following function is used, which can be implemented in PHP like this:

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] function RotateLeft($a, $n)
{
return ($a << $n) | ((($a >> 1) & 0x7fffffff) >> (31 - $ n)); Pay attention to the precedence of operators
}

Those who are more interested in encryption and decryption may have encountered the same problem as me. I wonder if anyone has a better method.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364403.htmlTechArticleThere are many scripting languages ​​that do not have unsinged int, such as PHP. Bit operations Generally speaking, there is no difference between int and unsinged int, except for the right shift operation, which is 1's complement in int...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template