voidupperToLower(std::string& s){ for (auto& c: s) { c |= (1 << 5); // tolower(c); } }
intmain(){ std::string s = "aBcDeFgHiJkLmN"; std::cout << "Before: " << s << '\n'; upperToLower(s); std::cout << "After : " << s << '\n'; return0; }
// Output: // Before: aBcDeFgHiJkLmN // After : abcdefghijklmn
小写字母转大写字母
ch &= '_' 或 ch |= ~(1 << 5), '_' 为 11011111
也可以直接使用 cctype 文件中的 toupper
#include<iostream> // #include <cctype>
voidlowerToUpper(std::string& s){ for (auto& c: s) { c &= ~(1 << 5); // toupper(c); } }
intmain(){ std::string s = "aBcDeFgHiJkLmN"; std::cout << "Before: " << s << '\n'; lowerToUpper(s); std::cout << "After : " << s << '\n'; return0; }
// Output: // Before: aBcDeFgHiJkLmN // After : ABCDEFGHIJKLMN
大写转小写同时小写转大写
要将一个字符串中的大小写字母格式互换,即大写字母转成小写字母,并且小写字母转成大写字母,直接将 bit 位第 5 位反转即可。
ch ^= (1 << 5)
#include<iostream>
voidchangeStrLowerUpper(std::string& s){ for (auto& c: s) { c ^= (1 << 5); } }
intmain(){ std::string s = "aBcDeFgHiJkLmN"; std::cout << "Before: " << s << '\n'; changeStrLowerUpper(s); std::cout << "After : " << s << '\n'; return0; }
// Output: // Before: aBcDeFgHiJkLmN // After : AbCdEfGhIjKlMn
计算数字 bit 位中 1 的个数
Brian Kernighan’s Algorithm
1 Initialize count: = 0 2 If integer n is not zero (a) Do bitwise & with (n-1) and assign the value back to n n: = n&(n-1) (b) Increment count by 1 (c) go to step 2 3 Else return count
intcountBitOf1(int num){ int count = 0; while (num) { num &= num - 1; count++; } return count; }
intmain(){ int num = 29; std::cout << "bits of num: "; printWithBit(num, 2); std::cout << "Count of 1: " << countBitOf1(num) << '\n';
return0; }
// Output: // bits of num: 00011101 // Count of 1: 4
计算 1 ~ n 的异或值
Number Binary-Repr XOR-from-1-to-n 1 1 [0001] <----- Always 1 as for n%4 = 1 2 10 [0011] <----- n + 1 3 11 [0000] <----- We get a 0 4 100 [0100] <----- Equals to n 5 101 [0001] 6 110 [0111] 7 111 [0000] <----- We get 0 8 1000 [1000] <----- Equals to n 9 1001 [0001] 10 1010 [1011] 11 1011 [0000] <------ We get 0 12 1100 [1100] <------ Equals to n
intcomputeXOR(int n){ if (n % 4 == 0) return n; if (n % 4 == 1) return1; if (n % 4 == 2) return n + 1; else return0; }
和与异或相等问题
给定一个正整数 n,找到正整数 i 的计数,使得 0 <= i <= n 且 n + i = n ^ i
Example
Input : n = 7 Output : 1 Explanation: 7^i = 7+i holds only for only for i = 0 7+0 = 7^0 = 7
Input n = 12 Output: 4 12^i = 12+i hold only for i = 0, 1, 2, 3 for i=0, 12+0 = 12^0 = 12 for i=1, 12+1 = 12^1 = 13 for i=2, 12+2 = 12^2 = 14 for i=3, 12+3 = 12^3 = 15
So n + i = n ^ i implies n & i = 0
Hence our problem reduces to finding values of i such that n & i = 0. How to find count of such pairs? We can use the count of unset-bits in the binary representation of n. For n & i to be zero, i must unset all set-bits of n. If the kth bit is set at a particular in n, kth bit in i must be 0 always, else kth bit of i can be 0 or 1
Hence, total such combinations are 2^(count of unset bits in n)
For example, consider n = 12 (Binary representation : 1 1 0 0).
All possible values of i that can unset all bits of n are 0 0 0/1 0/1 where 0/1 implies either 0 or 1. Number of such values of i are 2^2 = 4.
intcountValues(int n){ // unset_bits keeps track of count of un-set // bits in binary representation of n int unset_bits = 0; while (n) { if ((n & 1) == 0) unset_bits++; n = n >> 1; } // Return 2 ^ unset_bits return1 << unset_bits; }
intmain(){ int num = 29; std::cout << "bits of num: "; printWithBit(num, 2); std::cout << "Count of 1: " << countBitOf1(num) << '\n';
return0; }
快速交换两个数
a ^= b; b ^= a; a ^= b;
#include<iostream>
voidswap(int& a, int& b){ a = a ^ b; b = a ^ b; a = a ^ b; }
intmain(){ int a = 5, b = 9; std:: cout << "a = " << a << " b = " << b << '\n'; swap(a, b); std:: cout << "a = " << a << " b = " << b << '\n';