24小时热门版块排行榜    

查看: 764  |  回复: 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 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 292求调剂 +3 yhk_819 2026-02-28 3/150 2026-02-28 21:57 by gaoxiaoniuma
[考研] 304求调剂 +3 52hz~~ 2026-02-28 4/200 2026-02-28 21:41 by gaoxiaoniuma
[考研] 290求调剂 +5 材料专硕调剂; 2026-02-28 6/300 2026-02-28 21:40 by gaoxiaoniuma
[考研] 材料学调剂 +5 提神豆沙包 2026-02-28 5/250 2026-02-28 21:34 by gaoxiaoniuma
[考研] 材料类求调剂 +6 wana_kiko 2026-02-28 6/300 2026-02-28 21:20 by gaoxiaoniuma
[考研] 求调剂 +4 repeatt?t 2026-02-28 4/200 2026-02-28 21:16 by gaoxiaoniuma
[考研] 284求调剂 +4 天下熯 2026-02-28 4/200 2026-02-28 21:13 by gaoxiaoniuma
[考研] 085600材料工程一志愿中科大总分312求调剂 +8 吃宵夜1 2026-02-28 10/500 2026-02-28 20:27 by L135790
[基金申请] 面上模板改不了页边距吧? +5 ieewxg 2026-02-25 5/250 2026-02-28 20:11 by iwuli
[考研] 276求调剂 +3 路lyh123 2026-02-28 4/200 2026-02-28 19:45 by 路lyh123
[考博] 博士推荐 +5 花儿笑? 2026-02-21 6/300 2026-02-28 18:53 by nxgogo
[教师之家] 版面费该交吗 +15 苹果在哪里 2026-02-22 18/900 2026-02-28 18:20 by mibaomingg
[考研] 285求调剂 +5 满头大汗的学生 2026-02-28 5/250 2026-02-28 18:10 by 材料专硕调剂;
[考研] 材料调剂 +3 爱擦汗的可乐冰 2026-02-28 3/150 2026-02-28 18:06 by houyaoxu
[考博] 博士自荐 +3 kkluvs 2026-02-28 3/150 2026-02-28 16:59 by StarAura
[考研] 265分求调剂不调专业和学校有行学上就 +4 礼堂丁真258 2026-02-28 6/300 2026-02-28 16:18 by 求调剂zz
[考研] 0856调剂 +3 刘梦微 2026-02-28 3/150 2026-02-28 13:22 by houyaoxu
[考研] 寻找调剂 +3 LYidhsjabdj 2026-02-28 3/150 2026-02-28 12:59 by miniwendy
[考研] 272求调剂 +3 田智友 2026-02-28 3/150 2026-02-28 12:31 by 王加浩to
[考研] 298求调剂 +4 axyz3 2026-02-28 4/200 2026-02-28 11:21 by wang_dand
信息提示
请填处理意见