File tree Expand file tree Collapse file tree 5 files changed +21
-3
lines changed Expand file tree Collapse file tree 5 files changed +21
-3
lines changed Original file line number Diff line number Diff line change @@ -292,7 +292,7 @@ <h2 id="_1">前言</h2>
292
292
< blockquote >
293
293
< p > < img src ="./img/bulb.png " height ="30px " width ="auto " style ="margin: 0; border: none "/> 本书还在持续更新中……要追番的话,可以在 < a href ="https://github.com/parallel101/cppguidebook "> GitHub</ a > 点一下右上角的 “Watch” 按钮,每当小彭老师提交新 commit,GitHub 会向你发送一封电子邮件,提醒你小彭老师更新了。</ p >
294
294
</ blockquote >
295
- < p > 更新时间:2024年11月26日 12:33:25 (UTC+08:00)</ p >
295
+ < p > 更新时间:2024年12月17日 11:34:36 (UTC+08:00)</ p >
296
296
< p > < a href ="https://parallel101.github.io/cppguidebook "> 在 GitHub Pages 浏览本书</ a > | < a href ="https://142857.red/book "> 在小彭老师自己维护的镜像上浏览本书</ a > </ p >
297
297
< h2 id ="_2 "> 格式约定</ h2 >
298
298
< blockquote >
Original file line number Diff line number Diff line change @@ -421,7 +421,7 @@ <h2 id="index-_1">前言</h2>
421
421
<blockquote>
422
422
<p><img src="../img/bulb.png" height="30px" width="auto" style="margin: 0; border: none"/> 本书还在持续更新中……要追番的话,可以在 <a href="https://github.com/parallel101/cppguidebook">GitHub</a> 点一下右上角的 “Watch” 按钮,每当小彭老师提交新 commit,GitHub 会向你发送一封电子邮件,提醒你小彭老师更新了。</p>
423
423
</blockquote>
424
- <p>更新时间:2024年11月26日 12:33:25 (UTC+08:00)</p>
424
+ <p>更新时间:2024年12月17日 11:34:36 (UTC+08:00)</p>
425
425
<p><a href="https://parallel101.github.io/cppguidebook">在 GitHub Pages 浏览本书</a> | <a href="https://142857.red/book">在小彭老师自己维护的镜像上浏览本书</a></p>
426
426
<h2 id="index-_2">格式约定</h2>
427
427
<blockquote>
@@ -19203,6 +19203,15 @@ <h3 id="undef-_9">左移或右移的位数,不得超过整数类型上限,
19203
19203
i << 0; // 可以
19204
19204
i << -1; // 错!
19205
19205
</code></pre>
19206
+ <p>但是你还需要考虑一件事情:<strong>隐式转换</strong>,或者直接点说:<strong>整数提升</strong>。</p>
19207
+ <ul>
19208
+ <li>在 C++ 中算术运算符不接受小于 int 的类型进行运算。如果你觉得可以,那只是隐式转换,整形提升了。</li>
19209
+ </ul>
19210
+ <pre><code class="language-cpp">std::uint8_t c{ '0' };
19211
+ using T1 = decltype(c << 1); // int
19212
+ </code></pre>
19213
+ <p>即使移位大于等于 8 也不成问题。</p>
19214
+ <hr />
19206
19215
<p>对于有符号整数,左移还不得破坏符号位</p>
19207
19216
<pre><code class="language-cpp">int i = 0;
19208
19217
i << 1; // 可以
Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change @@ -583,6 +583,15 @@ <h3 id="_9">左移或右移的位数,不得超过整数类型上限,不得
583
583
i << 0; // 可以
584
584
i << -1; // 错!
585
585
</ code > </ pre >
586
+ < p > 但是你还需要考虑一件事情:< strong > 隐式转换</ strong > ,或者直接点说:< strong > 整数提升</ strong > 。</ p >
587
+ < ul >
588
+ < li > 在 C++ 中算术运算符不接受小于 int 的类型进行运算。如果你觉得可以,那只是隐式转换,整形提升了。</ li >
589
+ </ ul >
590
+ < pre > < code class ="language-cpp "> std::uint8_t c{ '0' };
591
+ using T1 = decltype(c << 1); // int
592
+ </ code > </ pre >
593
+ < p > 即使移位大于等于 8 也不成问题。</ p >
594
+ < hr />
586
595
< p > 对于有符号整数,左移还不得破坏符号位</ p >
587
596
< pre > < code class ="language-cpp "> int i = 0;
588
597
i << 1; // 可以
You can’t perform that action at this time.
0 commit comments