
C位运算技巧应用位运算是直接操作二进制位的运算具有极高的执行效率。掌握位运算技巧可以优化算法性能并实现紧凑的数据表示。基本位运算包括与、或、异或、取反和移位操作。#include#includevoid basic_bitwise_operations() {unsigned int a 0b1010;unsigned int b 0b1100;std::cout a: std::bitset4(a) ( a )\n;std::cout b: std::bitset4(b) ( b )\n;std::cout a b: std::bitset4(a b) ( (a b) )\n;std::cout a | b: std::bitset4(a | b) ( (a | b) )\n;std::cout a ^ b: std::bitset4(a ^ b) ( (a ^ b) )\n;std::cout ~a: std::bitset4(~a) \n;std::cout a 1: std::bitset4(a 1) ( (a 1) )\n;std::cout a 1: std::bitset4(a 1) ( (a 1) )\n;}位掩码用于设置、清除和检查特定位。void bit_mask_operations() {unsigned int flags 0;flags | (1 0);std::cout Set bit 0: std::bitset8(flags) \n;flags | (1 2);std::cout Set bit 2: std::bitset8(flags) \n;if (flags (1 2)) {std::cout Bit 2 is set\n;}flags ~(1 0);std::cout Clear bit 0: std::bitset8(flags) \n;flags ^ (1 2);std::cout Toggle bit 2: std::bitset8(flags) \n;}位运算可以实现快速的数学运算。void bitwise_math() {int x 15;int doubled x 1;std::cout x * 2 doubled \n;int halved x 1;std::cout x / 2 halved \n;int power_of_two 1 10;std::cout 2^10 power_of_two \n;bool is_even (x 1) 0;std::cout x is (is_even ? even : odd) \n;}位运算可以检测和操作2的幂。bool is_power_of_two(unsigned int n) {return n 0 (n (n - 1)) 0;}unsigned int next_power_of_two(unsigned int n) {n--;n | n 1;n | n 2;n | n 4;n | n 8;n | n 16;return n 1;}void power_of_two_operations() {std::cout 16 is power of 2: is_power_of_two(16) \n;std::cout 15 is power of 2: is_power_of_two(15) \n;std::cout Next power of 2 after 100: next_power_of_two(100) \n;}位计数统计二进制中1的个数。int count_bits(unsigned int n) {int count 0;while (n) {count n 1;n 1;}return count;}int count_bits_fast(unsigned int n) {int count 0;while (n) {n n - 1;count;}return count;}void bit_counting() {unsigned int n 0b10110101;std::cout Number: std::bitset8(n) \n;std::cout Bit count: count_bits(n) \n;std::cout Bit count (fast): count_bits_fast(n) \n;}位运算可以实现无临时变量的交换。void swap_without_temp(int a, int b) {if (a ! b) {a ^ b;b ^ a;a ^ b;}}void xor_swap() {int x 10, y 20;std::cout Before: x x , y y \n;swap_without_temp(x, y);std::cout After: x x , y y \n;}位域提供紧凑的数据存储。struct PackedData {unsigned int flag1 : 1;unsigned int flag2 : 1;unsigned int type : 3;unsigned int value : 11;};void bitfield_usage() {PackedData data;data.flag1 1;data.flag2 0;data.type 5;data.value 1234;std::cout Size: sizeof(PackedData) bytes\n;std::cout flag1: data.flag1 \n;std::cout type: data.type \n;std::cout value: data.value \n;}位运算可以实现高效的集合操作。class BitSet {unsigned int bits_;public:BitSet() : bits_(0) {}void add(int element) {bits_ | (1 element);}void remove(int element) {bits_ ~(1 element);}bool contains(int element) const {return (bits_ (1 element)) ! 0;}BitSet union_with(const BitSet other) const {BitSet result;result.bits_ bits_ | other.bits_;return result;}BitSet intersect_with(const BitSet other) const {BitSet result;result.bits_ bits_ other.bits_;return result;}void print() const {std::cout std::bitset32(bits_) \n;}};void bitset_operations() {BitSet set1, set2;set1.add(1);set1.add(3);set1.add(5);set2.add(3);set2.add(4);set2.add(5);std::cout Set1: ; set1.print();std::cout Set2: ; set2.print();BitSet union_set set1.union_with(set2);std::cout Union: ; union_set.print();BitSet intersect_set set1.intersect_with(set2);std::cout Intersection: ; intersect_set.print();}位运算是底层优化的重要工具在系统编程、图形处理和算法优化中广泛应用。