文章目录
1、判断数值出现的奇偶次
问题描述:
根据一个数组,查找出一个出现奇数次数的数,其余为偶数次数;
- 利用
^运算
,只要相同则等于0;
/*----------------------------------------------------------------------
> File Name: xorSel.cpp
> Author: Jxiepc
> Mail: Jxiepc
> Created Time: Wed 26 Jan 2022 10:41:48 AM CST
----------------------------------------------------------------------*/
#include <iostream>
int a[9] = {1, 2, 3, 2, 2, 2, 1, 1, 1};
int b[10] = {1, 1, 2, 3, 2, 2, 2, 1, 1, 1};
/** 根据一个数组,查找出一个出现奇数次数的数,其余为偶数次数 */
void exec1() {
int ret = 0;
for(int i=0; i<9; ++i){
ret ^= a[i];
}
std::cout << ret << std::endl;
}
/** 偶数次 */
void exec2() {
int ret = 0;
for(int i=0; i<10; ++i){
ret ^= b[i];
}
// 提取最右边的1
int rOne = ret & (~ret+1);
int rret = 0;
for(int i=0; i<10; ++i){
if((b[i] & rOne) == 0){
rret ^= b[i];
}
}
std::cout << rret << " & " << (ret ^ rret) << std::endl;
}
void test() {
exec2();
}
int main(int argc, char* argv[])
{
test();
return 0;
}
2、数值交换不需要用到临时变量
/*----------------------------------------------------------------------
> File Name: swap.cpp
> Author: Jxiepc
> Mail: Jxiepc
> Created Time: Wed 26 Jan 2022 08:33:54 AM CST
----------------------------------------------------------------------*/
/**
* 使用异或的方法对数值进行交换
* 前提:两个数值不能存在于一个内存地址,否则会为0
* */
#include <iostream>
template<class T>
void swap(T& a, T& b) {
a = a^b;
b = a^b;
a = a^b;
}
void test() {
int a = 5;
int b = 4;
std::cout << "交换前: a = " << a << " b = " << b << std::endl;
swap(a, b);
std::cout << "交换后: a = " << a << " b = " << b << std::endl;
}
int main(int argc, char* argv[])
{
test();
return 0;
}
3、获取相应内存块大小
/*----------------------------------------------------------------------
> File Name: round_up.cpp
> Author: Jxiepc
> Mail: Jxiepc
> Created Time: Sat 22 Jan 2022 08:09:19 PM CST
----------------------------------------------------------------------*/
#include <iostream>
/*
* @param bytes:需要的字节数
* @return: 返回对应的内存区块
* */
size_t round_up(size_t bytes){
return ((bytes) + 7) & ~(7);
}
void test() {
int a = 15;
std::cout << "【内存对齐】需要申请的内存大小" << round_up(a) << std::endl;
}
int main(int argc, char* argv[]) {
test();
return 0;
}
4、提取数值位数值
/*----------------------------------------------------------------------
> File Name: getBitNum.cpp
> Author: Jxiepc
> Mail: Jxiepc
> Created Time: Tue 01 Feb 2022 08:25:40 PM CST
----------------------------------------------------------------------*/
/**
* 获取数字对应数字
* */
#include <math.h>
#include <iostream>
/** 传入数值和对应的第几位 */
int getBitNum(int num, int b) {
return (int)(num/(pow(10, b-1))) % 10;
}
void test() {
int a = 123;
std::cout << getBitNum(123, 3) << " "
<< getBitNum(123, 2) << " "
<< getBitNum(123, 1) << " "
<< std::endl;
}
int main(int argc, char* argv[])
{
test();
return 0;
}
5、判断该数值是否为2或4的幂
/*----------------------------------------------------------------------
> File Name: bitTF.cpp
> Author: Jxiepc
> Mail: Jxiepc
> Created Time: Tue 15 Mar 2022 03:10:38 PM CST
----------------------------------------------------------------------*/
/**
* 如何判断该数值为2或4的幂
* */
#include <iostream>
/** 根据2的特性,二进制中只有一个位为1 */
bool isT() {
return (n & (n-1) == 0)
}
/** 满足只有一个1,且这个1在二进制中第1、3、5、7……上*/
bool isF() {
return (n& (n-1) == 0) && (n & 0x55555555) != 0;
}
int main(int argc, char* argv[])
{
return 0;
}
6、位操作数值乘除
/*----------------------------------------------------------------------
> File Name: bitPow.cpp
> Author: Jxiepc
> Mail: Jxiepc
> Created Time: Sun 06 Feb 2022 10:09:41 AM CST
----------------------------------------------------------------------*/
#include <iostream>
/*
* 乘法
* */
void test(int x, int d) {
int tmp = x << (d-1);
std::cout << tmp << std::endl;
}
/**
* 除法
* */
void test(int x, int d-1) {
int tmp = x >> (d-1);
std::cout << tmp << std::endl;
}
int main(int argc, char* argv[])
{
test(5, 2);
return 0;
}
7、位的状态操作bit数组
/*----------------------------------------------------------------------
> File Name: bitMap.cpp
> Author: Jxiepc
> Mail: Jxiepc
> Created Time: Thu 03 Mar 2022 11:14:50 AM CST
----------------------------------------------------------------------*/
#include <iostream>
template<class T>
class bitMap {
public:
bitMap() {
T a;
bitNum(a);
}
/** 数据类型的bit */
int bitNum(T a) {
std::cout << sizeof(a) * 8 << std::endl;
return sizeof(a) * 8;
}
/** 要取出的bit数,对应列表中的哪个数值 */
void setNumIdx(T bit) const {
numIdx = bit/m_bit;
}
int getNumIdx(T bit) {
return numIdx;
}
/** 取出的bit数种,对应列表中数值的哪一位上 */
void setbitIdx(T bit) const {
bitIdx = bit % m_bit;
}
int getbitIdx() {
return bitIdx;
}
/** 取出该位的状态 */
int getStatus() {
return ((arr[numIdx] >> (bitIdx)) &1);
}
/** 设置状态为0 */
void setFalse() {
arr[numIdx] = arr[numIdx] & (~ (1 << bitIdx));
}
/** 设置状态为1 */
void setTrue() {
arr[numIdx] = arr[numIdx] | (1<<bitIdx);
}
private:
int m_bit = 0;
int numIdx = -1;
int bitIdx = -1;
T arr[];
};
int main(int argc, char* argv[])
{
return 0;
}
8、不通过比较判断最大最小值
/*----------------------------------------------------------------------
> File Name: bitJudgeMax.cpp
> Author: Jxiepc
> Mail: Jxiepc
> Created Time: Tue 15 Mar 2022 03:02:09 PM CST
----------------------------------------------------------------------*/
/**
* 给定有符号32位整数,如何判断两个数的大小,不使用比较
* */
#include <iostream>
/** 将1->0;0->1 */
int filp(int n) {
return n ^ 1;
}
/** 判断是否为负数,非负数返回1 负数返回0 */
int sign(int n) {
return filp((n >> 31) & 1);
}
/** 获取最大值 */
int getMax(int a, int b) {
int c = a-b;
int A = sign(c);
int B = filp(A);
/** 经过判断后,以下小的值乘0即可返回最大值 */
return a * A + b * B;
}
int main(int argc, char* argv[])
{
int rt = getMax(4,1);
std::cout << rt << std::endl;
return 0;
}