怎么读一个一个字节的二进制位数数的位数

八位二进制数为什么表示范围是_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
八位二进制数为什么表示范围是
上传于|0|0|文档简介
阅读已结束,如果下载本文需要使用1下载券
想免费下载本文?
定制HR最喜欢的简历
你可能喜欢1814人阅读
C/C++(106)
#include &stdarg.h&
#include &stdio.h&
int ripple(int n, ...)
va_start(p, n);
for (; j & ++j)
i = va_arg(p, int);
for (; i &= i - 1)
va_end(p);
int main(void)
printf(&%d\n&, ripple(3, 5, 7));
The va_arg macro produces the arguments passed as the “...” part of a variadic function.& In
ripple it will be called twice, setting i first to 5, then to 7.
The expression i &= i - 1 resets the right-most 1 bit in
i.& For example, if i is 6 (binary 110), i & i - 1 is 4 (binary 100).& The inner
for loop executes until i is 0, so k is increased by the number of 1 bits in
There are two 1 bits in 5 (binary 101) and three in 7 (binary 111), so
ripple returns 5.
这道题也是两个知识点,一个是可变参数函数定义以及如何实现,va_arg会把5,7依次取出来。另一个知识点是i &= i-1,实际上是计算了i二进制形式中1的个数,每次计算都会消减掉最低有效位上的1。比如7二进制表示为111。i &= i –1的计算结果依次为110,100, 000 (也就是0)。在hacker's Delights这本书里介绍了很多类似技巧。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:510842次
积分:6249
积分:6249
排名:第3258名
原创:92篇
转载:165篇
评论:80条
(4)(1)(1)(1)(1)(1)(1)(4)(7)(11)(8)(2)(3)(2)(2)(2)(2)(2)(4)(4)(6)(11)(8)(2)(58)(8)(10)(8)(22)(17)(3)(16)(29)(3)查看: 14265|回复: 15|关注: 0
Matlab中怎样提取一个二进制数的某一位
<h1 style="color:# 麦片财富积分
新手, 积分 5, 距离下一级还需 45 积分
怎样提取一个二进制数的某一位
比如说有一个二进制数,怎么用程序提取这个数的第三位出来啊?
[ 本帖最后由 lskyp 于
10:52 编辑 ]
<h1 style="color:# 麦片财富积分
二进制在matlab里都是存成字符型变量的
a(5)就是从左到右第五个字符
<h1 style="color:# 麦片财富积分
从右往左,a(end-n)&&对应第n+1个字符
论坛优秀回答者
帖子最佳答案
关注者: 459
也有专门的函数bitget
C = bitget(uint8(6), 2)
论坛优秀回答者
帖子最佳答案
关注者: 459
对于楼主的问题,这样处理:
C = bitget(bin2dec(''), 3)
<h1 style="color:#9 麦片财富积分
细节决定成败!
关注者: 39
原帖由 hyowinner 于
13:42 发表
对于楼主的问题,这样处理:
C = bitget(bin2dec(''), 3)
谢谢帅哥 长知识了~~
<h1 style="color:#9 麦片财富积分
细节决定成败!
关注者: 39
&& bin2dec('')
&& bitget(bin2dec(''), 3)
这个怎么解释呢?
<h1 style="color:# 麦片财富积分
关注者: 8
bin2dec('')是将二进制转10进制。
bitget(bin2dec(''), 3),就是 取出十进制数的 二进制表示的第三位
加入分享快乐
<h1 style="color:# 麦片财富积分
可以直接这样吗
<h1 style="color:#2 麦片财富积分
关注者: 50
原帖由 hyowinner 于
13:42 发表
对于楼主的问题,这样处理:
C = bitget(bin2dec(''), 3)
好方法,学习一个。
站长推荐 /3
Powered by

我要回帖

更多关于 二进制位数 的文章

 

随机推荐