24小时热门版块排行榜    

查看: 726  |  回复: 3

benchurry

铜虫 (初入文坛)

[求助] c++位操作求助

《c++ primer》中5.3.1节
bitset_quiz1.set(27);
int_quiz1 ▏=1UL<<27;
我的疑惑是第一句显然是将bitset_quiz1中位置27(从0开始数)的位置为1,但是
1UL<<27就迷惑了,这个不是将32位表示的二进制数1左移27么,那它就是位置4置为1了(从0开始数)。这两个明显不一样嘛,我觉得是不是应该将
int_quiz1 ▏=1UL<<27;中27改为4啊??????
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

我是个好人
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

libralibra

至尊木虫 (著名写手)

骠骑将军

这是定义的那段话
    bitset<30> bitset_quiz1;     //  bitset solution
     unsigned long int_quiz1 = 0; // simulated collection of bits
In the bitset case we can define bitset_quiz1 to be exactly the size we need. By default each of the bits is set to zero. In the case where we use a built-in type to hold our quiz results, we define int_quiz1 as an unsigned long, meaning that it will have at least 32 bits on any machine. Finally, we explicitly initialize int_quiz1 to ensure that the bits start out with well-defined values.

这里是为了说明2个解决方案,要么用bitset,要么用unsigned long,都可以.

下面是设置第27个学生的成绩(都是从右往左数的)
     bitset_quiz1.set(27);   //  indicate student number 27 passed
     int_quiz1 |= 1UL<<27;   //  indicate student number 27 passed
第一个bitset直接.set就可以,注意第二个是个简写的位或操作(|=),1UL<<27之后,从右往左第27位置1,然后与int_quiz1按位与,可以将int_quiz1的第27位置1,而不影响其他学生的成绩.
matlab/VB/python/c++/Java写程序请发QQ邮件:790404545@qq.com
2楼2013-06-04 17:20:44
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

benchurry

铜虫 (初入文坛)

引用回帖:
2楼: Originally posted by libralibra at 2013-06-04 17:20:44
这是定义的那段话
    bitset<30> bitset_quiz1;     //  bitset solution
     unsigned long int_quiz1 = 0; // simulated collection of bits
In the bitset case we can define bitset_quiz1 to be ex ...

你说的意思我明白,我就是没搞清1UL<<27后的样子,现在想想,是我把高位与低位顺序搞错了 应该是从右往左低位到高位.不过还是感谢你!!
我是个好人
3楼2013-06-05 08:45:08
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zhanglinfeng

新虫 (小有名气)

【答案】应助回帖

★ ★
感谢参与,应助指数 +1
benchurry: 金币+2, ★★★很有帮助, 谢谢 2013-06-05 12:12:04
1UL << 27是和.set(27)一样的意思,前面的相当与把1往前移动了27位,也就是.set(27).
4楼2013-06-05 10:44:24
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 benchurry 的主题更新
信息提示
请填处理意见