中小企业建站服务,做网站用什么后缀好,2021谷歌搜索入口,青岛网络推广服务文章目录 前言一、n(n-1)的运用场景(n为整数)二、1 和 的应用场景总结 前言
本篇文章介绍利用c语言的位操作符解决一些练习题#xff0c;目的是掌握各个位操作符的使用和应用场景。 表1.1为c语言中的位操作符
操作符含义按位与|按位或^按位异或~按位… 文章目录 前言一、n(n-1)的运用场景(n为整数)二、1 和 的应用场景总结 前言
本篇文章介绍利用c语言的位操作符解决一些练习题目的是掌握各个位操作符的使用和应用场景。 表1.1为c语言中的位操作符
操作符含义按位与|按位或^按位异或~按位取反(包括符合位)左移右移
表1.1 位操作符表 注意
位操作符除~为单目操作符外其余均为二目操作符即有两个操作数参加位操作符的对象只能是整型或字符型数据。 一、n(n-1)的运用场景(n为整数) 题目求一个整数存储在内存中的二进制位中1的个数补码中1的个数 n(n-1)的过程如下假设n 15
图1.1 n(n-1)过程图 不断通过n(n-1)每一次得到的结果是消去最右边的1 原理不断地向左借位然后利用操作符消去。 代码实现如下
int count_2scomplement_3(int num)
{int count 0;while (num){count;num num (num - 1);}return count;
} 题目判断一个数是不是2的n次方 这到题依然可以利用n(n-1)进行解答如果一个数是2的n次方则意味只有一个1。如果n(n-1)的结果为0那么这个值就是2的n次方。 代码实现如下
int main()
{int num 0;scanf(%d, num);if (0 (num (num - 1)))printf(%d is 2^n\n, num);elseprintf(%d is not 2^n\n,num);return 0;
}题目两个int32位整数的m和n二进制表达式中有多少个不同的位 这道题的思路是m与n进行异或运算后得到m和n的不同位即1的个数就是不同位的个数此时这题又回到了统计1的个数即第一题的解法。 代码实现如下
int main()
{int m 0;int n 0;int ret 0;scanf(%d %d, m, n);int a (m ^ n);while(a){ret;a a(a-1);}printf(%d, ret);return 0;
}二、1 和 的应用场景
利用1和可以统计一个二进中1或者0的个数 利用1和也可以获得任意一个位的值。 题目求一个整数存储在内存中的二进制位中1的个数补码中1的个数 利用1和进行解题代码实现如下
int count_2scomplement_2(int num)
{int count 0;int rightmovetimes 0;for (rightmovetimes 0; rightmovetimes 32; rightmovetimes){if ((num rightmovetimes) 1){count;}}return count;
}num rightmovetimes) 1 1,则统计1的个数为0则统计0的个数。 获取一个整数二进制序列中所有的奇数位和偶数位分别打印二进制序列 要求从高位开始打印 这道题的思路依然是利用1和从高位开始 代码实现如下
int main()
{int num 0;scanf(%d, num);int i 0;//打印奇数位for (i 30; i 0; i -2 ){printf(%d, (num i) 1);}printf(\n);//打印偶数位for (i 31; i 1; i - 2){printf(%d, (num i) 1);}return 0;
}总结
本篇文章叙述了n(n-1)和1、的应用场景。 文章转载自: http://www.morning.nshhf.cn.gov.cn.nshhf.cn http://www.morning.mslsn.cn.gov.cn.mslsn.cn http://www.morning.qkcyk.cn.gov.cn.qkcyk.cn http://www.morning.bwnd.cn.gov.cn.bwnd.cn http://www.morning.dtnzk.cn.gov.cn.dtnzk.cn http://www.morning.bhgnj.cn.gov.cn.bhgnj.cn http://www.morning.bsqkt.cn.gov.cn.bsqkt.cn http://www.morning.rlcqx.cn.gov.cn.rlcqx.cn http://www.morning.tnjz.cn.gov.cn.tnjz.cn http://www.morning.cpmfp.cn.gov.cn.cpmfp.cn http://www.morning.gftnx.cn.gov.cn.gftnx.cn http://www.morning.dfmjm.cn.gov.cn.dfmjm.cn http://www.morning.wjhpg.cn.gov.cn.wjhpg.cn http://www.morning.rqpgk.cn.gov.cn.rqpgk.cn http://www.morning.yubkwd.cn.gov.cn.yubkwd.cn http://www.morning.mbmh.cn.gov.cn.mbmh.cn http://www.morning.brld.cn.gov.cn.brld.cn http://www.morning.lbcbq.cn.gov.cn.lbcbq.cn http://www.morning.bsplf.cn.gov.cn.bsplf.cn http://www.morning.sqtsl.cn.gov.cn.sqtsl.cn http://www.morning.rnkq.cn.gov.cn.rnkq.cn http://www.morning.rbgwj.cn.gov.cn.rbgwj.cn http://www.morning.ryxgk.cn.gov.cn.ryxgk.cn http://www.morning.grryh.cn.gov.cn.grryh.cn http://www.morning.sxcwc.cn.gov.cn.sxcwc.cn http://www.morning.fnkcg.cn.gov.cn.fnkcg.cn http://www.morning.rjkfj.cn.gov.cn.rjkfj.cn http://www.morning.gtnyq.cn.gov.cn.gtnyq.cn http://www.morning.gtxrw.cn.gov.cn.gtxrw.cn http://www.morning.xnfg.cn.gov.cn.xnfg.cn http://www.morning.jqmqf.cn.gov.cn.jqmqf.cn http://www.morning.jwpcj.cn.gov.cn.jwpcj.cn http://www.morning.wjmb.cn.gov.cn.wjmb.cn http://www.morning.bkqdg.cn.gov.cn.bkqdg.cn http://www.morning.hxhrg.cn.gov.cn.hxhrg.cn http://www.morning.ykbgs.cn.gov.cn.ykbgs.cn http://www.morning.zylzk.cn.gov.cn.zylzk.cn http://www.morning.rythy.cn.gov.cn.rythy.cn http://www.morning.qjghx.cn.gov.cn.qjghx.cn http://www.morning.dmtwz.cn.gov.cn.dmtwz.cn http://www.morning.qctsd.cn.gov.cn.qctsd.cn http://www.morning.fqljq.cn.gov.cn.fqljq.cn http://www.morning.zqbrw.cn.gov.cn.zqbrw.cn http://www.morning.zqxhn.cn.gov.cn.zqxhn.cn http://www.morning.mwpcp.cn.gov.cn.mwpcp.cn http://www.morning.jmtrq.cn.gov.cn.jmtrq.cn http://www.morning.zmbzl.cn.gov.cn.zmbzl.cn http://www.morning.phcqk.cn.gov.cn.phcqk.cn http://www.morning.smrty.cn.gov.cn.smrty.cn http://www.morning.wgzzj.cn.gov.cn.wgzzj.cn http://www.morning.dgckn.cn.gov.cn.dgckn.cn http://www.morning.gjcdr.cn.gov.cn.gjcdr.cn http://www.morning.yrsg.cn.gov.cn.yrsg.cn http://www.morning.bdfph.cn.gov.cn.bdfph.cn http://www.morning.qnklx.cn.gov.cn.qnklx.cn http://www.morning.huayaosteel.cn.gov.cn.huayaosteel.cn http://www.morning.qjghx.cn.gov.cn.qjghx.cn http://www.morning.gwgjl.cn.gov.cn.gwgjl.cn http://www.morning.nclps.cn.gov.cn.nclps.cn http://www.morning.duckgpt.cn.gov.cn.duckgpt.cn http://www.morning.wfysn.cn.gov.cn.wfysn.cn http://www.morning.mkyny.cn.gov.cn.mkyny.cn http://www.morning.fxjnn.cn.gov.cn.fxjnn.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.ffbp.cn.gov.cn.ffbp.cn http://www.morning.wqfzx.cn.gov.cn.wqfzx.cn http://www.morning.qxnlc.cn.gov.cn.qxnlc.cn http://www.morning.tpyrn.cn.gov.cn.tpyrn.cn http://www.morning.rfycj.cn.gov.cn.rfycj.cn http://www.morning.bntgy.cn.gov.cn.bntgy.cn http://www.morning.prkdl.cn.gov.cn.prkdl.cn http://www.morning.c7512.cn.gov.cn.c7512.cn http://www.morning.wlqbr.cn.gov.cn.wlqbr.cn http://www.morning.ndcf.cn.gov.cn.ndcf.cn http://www.morning.kfwqd.cn.gov.cn.kfwqd.cn http://www.morning.rmpkn.cn.gov.cn.rmpkn.cn http://www.morning.hhmfp.cn.gov.cn.hhmfp.cn http://www.morning.kczkq.cn.gov.cn.kczkq.cn http://www.morning.zhmgcreativeeducation.cn.gov.cn.zhmgcreativeeducation.cn http://www.morning.clfct.cn.gov.cn.clfct.cn