Algorithms series - bit computing

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/ydm19891101/article/details/95750790

table of Contents

I. Introduction

Second, understand the concept of

Third, the skills and combat

3.1 Common Skills

3.2 combat

3.2.1 O (1) time complexity of determining the number is not a power of two

3.2.2 calculates a 32-bit binary representation of an integer number of 1-

3.2.3 integer A conversion is B, the number of bits required to change the bit

3.2.4 array, a number appears only once, and the rest appear twice, once to find the emergence of elements

3.2.5 array, two numbers appear only once, and the rest appear twice, once to find the emergence of two elements.

3.2.6 determining whether a number is an even number


 

I. Introduction

Bit computing in daily programming is a problem-solving tool, and this tool is often ignored by most people, today we have come together to learn about.

Note: The following code provides PHP language version only.

Second, understand the concept of

Operators Explanation Examples Remark
& (AND operation) If the two corresponding bits are 1, the result of the bit is 1, otherwise 0

0 & 1 = 0

1 & 1 = 1

0 & x = 0
| (OR) Two respective bits as long as there is a 1, the bit value of 1 results

1 | 0 = 1

0 | 0 = 0

1 | 1 = 1

1 | x = 1
^ (XOR operation) If the two bits of the same value participate in operations, compared with 0, 1 otherwise

1 ^ 1 = 0

0 ^ 0 = 0

1 ^ 0 = 1

If x ^ y = z, then x ^ z = y;

y ^ z = x

- (the negation operator)

~ Is a unary operator, is used for a binary bitwise, i.e. 0 to 1, 1

~0 = 1

~1 = 0

 
 << (left) For each of a number of bits of all N-bit left shift, a right complement 0

<<1 = 3

 
>> (right) Each N-bit binary bit-right of a number, to the right end of the low are discarded, for the unsigned number, high fill 0 3>> = 1  

Third, the skills and combat

 

3.1 Common Skills

  •  x & (x-1): eliminate the last bit x 1
  • a ^ b ^ b = a: element deduplication
  • If x ^ y = z; the ^ z = y x; z ^ y = x 

 

3.2 combat

3.2.1 O (1) time complexity of determining the number is not a power of two

Analysis: If the number is a power of two, it must satisfy the following two conditions

1) The number is greater than 0;

2) the number of binary representation of only the highest bit is 1

Tip: Use x & (x -1) eliminated the highest level of 1, 0 if the number is a power of two, not otherwise

Code:

<?php

checkMin(3);
function checkMin($num) {
	if ($num <= 0) {
		return 0;
	}
	return intval($num & ($num - 1)) == 0 ? 1 : 0;
}

3.2.2 calculates a 32-bit binary representation of an integer number of 1-

Tips: using x & (x -1) erasing a most significant bit, i.e., calculation of the number of cancellation of a number of

Code:

<?php
getINum(7); 
function getINum($num) {
 	$count = 0;
 	while ($num> 0) {
 		$count ++;
 		$num = $num & ($num - 1);
 	}
 	return $count;
}

3.2.3 integer A conversion is B, the number of bits required to change the bit

Analysis: number 1, i.e. the second question two integers XOR operation, problems will eventually converted to present the results of the XOR

Code:

<?php
getDiffBit(4,5); 
function getDiffBit($a, $b) {
 	$count = 0;
 	if ($a == $b) {
 		return $count;
 	}
 	$diff = $a ^ $b;
 	while ($diff > 0) {
 		$count ++;
 		$diff = $diff & ($diff - 1);
 	}
 	return $count;
 }

3.2.4 array, a number appears only once, and the rest appear twice, once to find the emergence of elements

Analysis: a ^ b ^ b deduplication elements

<?php
$arr = array(1,2,3,1,2,3,4);
getSingle($arr);

function getSingle($arr) {
	$singleItem = null;
	for($i = 0; $i < count($arr); $i ++) {
		$singleItem ^= $arr[$i];
	}
	return $singleItem;
}


3.2.5 array, two numbers appear only once, and the rest appear twice, once to find the emergence of two elements.

Analysis: The results of all elements of the array of exclusive-or result of that is, two elements appear only once. These two elements not want to wait, so the XOR result is that there must be 1 in a seat. Locate the last occurrence of 1, and according to this position, again XOR array elements, will obtain a final two elements; reuse after x ^ xor = y stars another element

Code:

<?php
$arr =  array(1,1,2,2,4,5);
getSingleEle($arr);

 //因为涉及到了数组的循环,时间复杂度为O(n)
 function getSingleEle($arr) {
 	$xor = null;
 	$a = null;
 	$b = null;
 	//step 1:将数组元素进行异或操作,最终会得到只出现一次的两个元素的异或结果
 	//因为两个元素都只出现了一次,异或的结果是一定存在1元素
 	for ($i = 0; $i < count($arr); $i ++) {
 		$xor ^= $arr[$i]; 
 	}
 	
 	//step 2:寻找异或结果中最右侧1所在的位置
 	$position = 0;
 	$temp = $xor;
 	while($temp & 1 == 0) {
 		$position ++;
 		$temp >> 1;
 	}
 	
 	//step 3:根据step2 中1所在的位置,获取2个元素中position位置为bit为1的元素
 	//最终得到元素x
 	for ($i = 0; $i < count($arr); $i ++) {
 		if (($arr[$i] >> $position) & 1) {
 			$a ^= $arr[$i]; 
 		}
 	}
 	
 	//step 4: 根据x ^ xor = y公式得到y
 	$b = $a ^ $xor; 
 	echo $a . '_' .$b;
 }

3.2.6 determining whether a number is an even number

Analysis: If a number is even, its lowest level certain bits of zero.

Code:

<?php
$a = 11;
$bool = ($a & 1) == 0 ? 1 : 0;

 

Guess you like

Origin blog.csdn.net/ydm19891101/article/details/95750790