当前位置:网站首页>如何判断Int型值的第nbit位是否是1还是0
如何判断Int型值的第nbit位是否是1还是0
2022-04-21 19:47:00 【星光技术人】
- 如何判断Int型值的第nbit位是否是1还是0
bool nums_bit(int Num,int n)
{
if(N1%pow(2,n)/pow(2,n-1) <1.0)
return false;
return true;
}
- 题目
一组数有两个数的个数是奇数,其他数个数是偶数,取出个数是奇数的两个数
这道题是一道题的变形:一组数中有一个数的个数是奇数,其他数个数都是偶数;
使用异或运算求解;
异或运算是位运算,相同为0,不同为1;那么如果偶数个A进行异或,组后结果一定位0;0与B异或得B
根据异或运算得运算规律:
交换律 A^B = B^A
结合律 A^(B^C) = (A^B)^C
同一批数,异或运算得时候可以以任意顺序进行结合
- code
1. 所有元素进行异或运算,加入元素a,b的个数为奇数
int eor = 0;
for(int i=0;i<Arr.size();i++)
eor ^= Arr[i];
2. 得到的eor=a^b,因为a不等于b,所以a与b中的某个数至少在某一位为1(假如是第k位)
所以定义新变量onlyone遍历异或所有第k位是1的元素,最后得到的结果是a,b中的其中一个
int idx = eor & (~eor+1);//取最右边的1
int onlyone = 0;
for(auto num:Arr)
{
if(num & idx == 0)//遍历第k位不是1的元素,进行异或
onlyone ^= num;
}
cout<<onlyone<<" "<<onlyone^eor<<endl;
- 知识辨析
- 异或运算的本质
假如说一个数组中有3个数,计算3个数的异或结果;int型变量位32位,实际上是遍历这32个位置上的1的个数,某个位置上1的个数位偶数,这个位数异或结果为0;所以异或的本质是每一个bit位上的1个数的计算,与元素在数组中的顺序以及异或顺序无关。所以对于一堆数,异或遍历的顺序无关紧要。这就是力扣136题:只出现一次的数据题目的由来
版权声明
本文为[星光技术人]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qhu1600417010/article/details/124328414
边栏推荐
- High end manufacturing enterprise informatization solution, predictive maintenance of industrial e-commerce platform equipment, data and system
- SAP MTS/MTO/ETO专题之七:Q+M模式前后台操作
- 杰理之复位IO维持电平使用说明【篇】
- Digital business cloud community property platform system solution - easy property management and leveraging potential business opportunities
- iMeta | EndNote调整完美引文格式教程(视频)
- WLAN Qpower 介绍
- C# 将dll打包到程序中
- DolphinDB VSCode 插件使用教程
- 2023年南开大学税务专硕考研上岸前辈备考经验指导
- MKL library matrix multiplication
猜你喜欢

WLAN Qpower 介绍

Interpretation of Chinese ner using lattice LSTM paper

int count= cmd. ExecuteNonQuery(); There is a syntax error nearby

【2021】腾讯秋招技术岗编程 有效序列的数量
Learn MySQL performance tuning and make your database smooth

界面组件Telerik UI for WPF入门指南 - 颜色主题生成器

Static link and dynamic link

图像中stride的含义

高性能分布式缓存Redis--- Redis底层结构和缓存原理 --- 持续更新
![Sword finger offer: [day 29 dynamic programming (difficult)] --- > n dice points](/img/fe/1691eda08b8dbda532abc716d09143.png)
Sword finger offer: [day 29 dynamic programming (difficult)] --- > n dice points
随机推荐
Leetcode0886. 可能的二分法(medium,二分图)
知道创宇发布重磅战略方案,构建持续交火的实战化防御体系
Lenovo promises that 100% of all computer products will contain recycled plastics by 2025
杰理之中断列表添加【篇】
危化品企业双预防机制数字化建设综合解决方案
Experience and guidance of seniors preparing for the postgraduate entrance examination of labor health and environmental hygiene of Southeast University in 2023
内置注解讲解
PE市盈率们之间的区别
Digital business cloud: analyze the current situation of enterprise procurement management and promote the optimization and upgrading of enterprise procurement mode
使用CMake构建/导入目标
R语言数据分析从入门到高级:(八)数据清洗技巧之数据格式转换(包含宽数据与长数据之间的转换)
06.适配器模式
杰理之VDDIO_SYSVDD_DCDC14系统电压配置说明【篇】
2023年南开大学税务专硕考研上岸前辈备考经验指导
在多文件中C语言中全局变量的重定义
iMeta | EndNote调整完美引文格式教程(视频)
Rapid generation of latex from excel tables
Solutions informatiques pour les entreprises manufacturières haut de gamme, maintenance prédictive de l'équipement, des données et du système de la plate - forme de commerce électronique industriel
2023年东南大学劳动卫生与环境卫生学考研上岸前辈备考经验指导
auto 关键字的使用