-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
331 lines (192 loc) · 36.2 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>这是Harry的个人博客站</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta property="og:type" content="website">
<meta property="og:title" content="这是Harry的个人博客站">
<meta property="og:url" content="https://harryis666.github.io/index.html">
<meta property="og:site_name" content="这是Harry的个人博客站">
<meta property="og:locale" content="en_US">
<meta property="article:author" content="Harry Li">
<meta name="twitter:card" content="summary">
<link rel="alternate" href="/atom.xml" title="这是Harry的个人博客站" type="application/atom+xml">
<link rel="shortcut icon" href="/favicon.png">
<link rel="stylesheet" href="/css/style.css">
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">
<meta name="generator" content="Hexo 7.3.0"></head>
<body>
<div id="container">
<div id="wrap">
<header id="header">
<div id="banner"></div>
<div id="header-outer" class="outer">
<div id="header-title" class="inner">
<h1 id="logo-wrap">
<a href="/" id="logo">这是Harry的个人博客站</a>
</h1>
</div>
<div id="header-inner" class="inner">
<nav id="main-nav">
<a id="main-nav-toggle" class="nav-icon"><span class="fa fa-bars"></span></a>
<a class="main-nav-link" href="/">Home</a>
<a class="main-nav-link" href="/archives">Archives</a>
</nav>
<nav id="sub-nav">
<a class="nav-icon" href="/atom.xml" title="RSS Feed"><span class="fa fa-rss"></span></a>
<a class="nav-icon nav-search-btn" title="Search"><span class="fa fa-search"></span></a>
</nav>
<div id="search-form-wrap">
<form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit"></button><input type="hidden" name="sitesearch" value="https://Harryis666.github.io"></form>
</div>
</div>
</div>
</header>
<div class="outer">
<section id="main">
<article id="post-CASPP-Lab-第一章题解-1" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
<div class="article-meta">
<a href="/2024/09/23/CASPP-Lab-%E7%AC%AC%E4%B8%80%E7%AB%A0%E9%A2%98%E8%A7%A3-1/" class="article-date">
<time class="dt-published" datetime="2024-09-23T12:13:37.000Z" itemprop="datePublished">2024-09-23</time>
</a>
<div class="article-category">
<a class="article-category-link" href="/categories/CSAPP/">CSAPP</a>
</div>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="p-name article-title" href="/2024/09/23/CASPP-Lab-%E7%AC%AC%E4%B8%80%E7%AB%A0%E9%A2%98%E8%A7%A3-1/">CASPP Lab 第一章题解(1)</a>
</h1>
</header>
<div class="e-content article-entry" itemprop="articleBody">
<h1 id="Data-Lab"><a href="#Data-Lab" class="headerlink" title="Data Lab"></a>Data Lab</h1><h2 id="bitXor"><a href="#bitXor" class="headerlink" title="bitXor"></a>bitXor</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//1</span></span><br><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> * bitXor - x^y using only ~ and & </span></span><br><span class="line"><span class="comment"> * Example: bitXor(4, 5) = 1</span></span><br><span class="line"><span class="comment"> * Legal ops: ~ &</span></span><br><span class="line"><span class="comment"> * Max ops: 14</span></span><br><span class="line"><span class="comment"> * Rating: 1</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">bitXor</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span> {</span><br><span class="line"> <span class="keyword">return</span> (x&~y)|(~x&y);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>蛮简单的,直接异或</p>
<blockquote>
<p>有无更简便的写法呢?</p>
</blockquote>
<h2 id="tmin"><a href="#tmin" class="headerlink" title="tmin"></a>tmin</h2><p>关于<code>tmin</code></p>
<p>我的想法就是</p>
<ul>
<li>对0x00逻辑取反,得到111…111</li>
<li>然后右移一位,得到011…111</li>
<li>再加1,即可得到100…000这种形式</li>
</ul>
<p>有个要注意的,就是逻辑取反必须是对无符号数,因此我们首先要写上<code>unsigned</code></p>
<blockquote>
<p>这个地方还不确定,究竟什么时候需要加<code>unsigned</code></p>
</blockquote>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> * tmin - return minimum two's complement integer </span></span><br><span class="line"><span class="comment"> * Legal ops: ! ~ & ^ | + << >></span></span><br><span class="line"><span class="comment"> * Max ops: 4</span></span><br><span class="line"><span class="comment"> * Rating: 1</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">tmin</span><span class="params">(<span class="type">void</span>)</span> {</span><br><span class="line"> <span class="keyword">return</span> ( ( ~(<span class="type">unsigned</span>)<span class="number">0x00</span> ) >> <span class="number">1</span> ) + <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="isTmax"><a href="#isTmax" class="headerlink" title="isTmax"></a>isTmax</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//2</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * isTmax - returns 1 if x is the maximum, two's complement number,</span></span><br><span class="line"><span class="comment"> * and 0 otherwise </span></span><br><span class="line"><span class="comment"> * Legal ops: ! ~ & ^ | +</span></span><br><span class="line"><span class="comment"> * Max ops: 10</span></span><br><span class="line"><span class="comment"> * Rating: 1</span></span><br><span class="line"><span class="comment"> */</span></span><br></pre></td></tr></table></figure>
<p>不能用if之类的控制语句,==这类也不能用,因此需要好好思考思考<br>我们可以先得出最大的数,其实最大数也知道,就是01111…111这样的,我们用一个mask应该可以<br>如果和mask进行异或,得到0,说明是相同的,那就取反好了,反之就不是0;如何对于0输出1,对于其他数输出0?</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">isTmax</span><span class="params">(<span class="type">int</span> x)</span>{</span><br><span class="line"> <span class="type">unsigned</span> mask = ~(<span class="type">unsigned</span>)<span class="number">0x00</span> >> <span class="number">1</span>;<span class="comment">//得到0111..11</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span> - (<span class="type">bool</span>)( x ^ mask);<span class="comment">//异或的话,如果位相同则返回0, 位不同则返回1 </span></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>但是,bool类型不能用,这是c…</p>
<p>好吧,忘记了一个重要的事情,那就是——没有bool我可以用<code>!</code>啊…</p>
<p>那么<code>!</code>怎么实现呢?(这就是第9题的事情了)</p>
<p>下面是正确代码</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">isTmax</span><span class="params">(<span class="type">int</span> x)</span>{</span><br><span class="line"> <span class="type">unsigned</span> mask = ~(<span class="type">unsigned</span>)<span class="number">0x00</span> >> <span class="number">1</span>;<span class="comment">//得到0111..11</span></span><br><span class="line"> <span class="keyword">return</span> ! ( x ^ mask);<span class="comment">//异或的话,如果位相同则返回0, 位不同则返回1 </span></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<blockquote>
<p>看了一个讲解,还有一个思路:</p>
<p>目前我们能用的一个确定的数就是0,因此如果需要执行比较判断,我们可以试着从当前数通过一系列操作映射到目标数0<br>现在就有一个问题,执行操作的时候,可能会同时将其他一些数也映射到0,这个过程大概率不是一一对应的,当然也有可能hhh,可以想一想<br>总之,对于那些多出来的数,我们需要设计一些方法将它们排除掉</p>
</blockquote>
<h2 id="allOddBits"><a href="#allOddBits" class="headerlink" title="allOddBits"></a>allOddBits</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> * allOddBits - return 1 if all odd-numbered bits in word set to 1</span></span><br><span class="line"><span class="comment"> * where bits are numbered from 0 (least significant) to 31 (most significant)</span></span><br><span class="line"><span class="comment"> * Examples allOddBits(0xFFFFFFFD) = 0, allOddBits(0xAAAAAAAA) = 1</span></span><br><span class="line"><span class="comment"> * Legal ops: ! ~ & ^ | + << >></span></span><br><span class="line"><span class="comment"> * Max ops: 12</span></span><br><span class="line"><span class="comment"> * Rating: 2</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="comment">//直接告诉你了,一共32位,确实得告诉,不过假如不知道上限的话,怎么去做呢?或者怎么用这些写出循环判断呢?</span></span><br><span class="line"><span class="comment">//所有奇数位为1,mask一下?先做一个奇数位的mask,然后进行&的操作,我们就取出了奇数位,1,3,</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">allOddBits</span><span class="params">(<span class="type">int</span> x)</span> {</span><br><span class="line"> <span class="keyword">return</span> !(<span class="number">0xAAAAAAAA</span> ^ (x & <span class="number">0xAAAAAAAA</span>));</span><br><span class="line">}</span><br><span class="line"><span class="comment">//通过了,可以可以</span></span><br><span class="line"><span class="comment">//如果不知道上限位数呢?那么这个mask就需要我们自己去获取</span></span><br><span class="line"><span class="comment">//其实很容易,我们可以构建从mask到0的映射,然后去掉杂项即可</span></span><br><span class="line"><span class="comment">//映射为:x + x >> 1 = -1 = 1111...1111</span></span><br><span class="line"><span class="comment">//这个好理解,因为101010...1010这种,加上自己右移一位的结果,恰好就是全1</span></span><br><span class="line"><span class="comment">//那么倒推呢,还有谁可以通过这个映射计算出全1?</span></span><br><span class="line"><span class="comment">//稍微推导一下就知道,没有别的了,因为最低位相加为1,每一位对应相加为1,因此必然是0101的,而首位直接等于1(过程无进位),就推出x必然为1010...1010的形式</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">allOddBits</span><span class="params">(<span class="type">int</span> x)</span>{</span><br><span class="line"> <span class="keyword">return</span> !(<span class="number">-1</span> ^ (x + x >> <span class="number">1</span>));</span><br><span class="line">}</span><br><span class="line"><span class="comment">//其实自己想错了,构建映射然后去掉杂项,只能应用于判断条件,而非求值</span></span><br><span class="line"><span class="comment">//可是这个映射下只有1010...1010可以映射到0,能否映射回去呢?看看这个过程是不是双射,感觉就是吧,一个x对应一个结果,而结果为0的情况下只有x可以满足,就是双射</span></span><br><span class="line"><span class="comment">//尝试构建逆运算</span></span><br><span class="line"><span class="comment">//没搞出来,也许可以直接从Tmax下手,直接搞算数运算,之后想到了再试试吧</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//其实有个问题,即实验要求只能使用两位16进制,即0x00这种,因此0xAAAAAAAA需要通过0xAA来构造,倒是不麻烦</span></span><br></pre></td></tr></table></figure>
<h2 id="negate"><a href="#negate" class="headerlink" title="negate"></a>negate</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* negate - return -x </span></span><br><span class="line"><span class="comment"> * Example: negate(1) = -1.</span></span><br><span class="line"><span class="comment"> * Legal ops: ! ~ & ^ | + << >></span></span><br><span class="line"><span class="comment"> * Max ops: 5</span></span><br><span class="line"><span class="comment"> * Rating: 2</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="comment">//要求负数,我加个负号?</span></span><br><span class="line"><span class="comment">//好好好,还真能过,有点懵逼,真的</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">negate</span><span class="params">(<span class="type">int</span> x)</span> {</span><br><span class="line"> <span class="keyword">return</span> -x;</span><br><span class="line">}</span><br><span class="line"><span class="comment">//其实不对,没有满足要求</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">negate</span><span class="params">(<span class="type">int</span> x)</span> {</span><br><span class="line"> <span class="keyword">return</span> ~x + <span class="number">1</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="isAsciiDigit"><a href="#isAsciiDigit" class="headerlink" title="isAsciiDigit"></a>isAsciiDigit</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//3</span></span><br><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> * isAsciiDigit - return 1 if 0x30 <= x <= 0x39 (ASCII codes for characters '0' to '9')</span></span><br><span class="line"><span class="comment"> * Example: isAsciiDigit(0x35) = 1.</span></span><br><span class="line"><span class="comment"> * isAsciiDigit(0x3a) = 0.</span></span><br><span class="line"><span class="comment"> * isAsciiDigit(0x05) = 0.</span></span><br><span class="line"><span class="comment"> * Legal ops: ! ~ & ^ | + << >></span></span><br><span class="line"><span class="comment"> * Max ops: 15</span></span><br><span class="line"><span class="comment"> * Rating: 3</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="comment">//0x30 = 0011 0000</span></span><br><span class="line"><span class="comment">//0x39 = 0011 1001</span></span><br><span class="line"><span class="comment">//判断最高位是否为第6位且第5第6位是否为1,构建一个mask,0011 0000,然后结果右移4位,要是非0就说明不是,用一个flag记录一下</span></span><br><span class="line"><span class="comment">//然后若4为1则mask一下23,若4不为1则直接输出1</span></span><br><span class="line"><span class="comment">//看看咋写</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">isAsciiDigit</span><span class="params">(<span class="type">int</span> x)</span> {</span><br><span class="line"> <span class="type">int</span> flag_1 = !( (<span class="number">0x30</span> ^ x) >> <span class="number">4</span> );</span><br><span class="line"> <span class="type">int</span> a = (<span class="number">0x08</span> & x) >> <span class="number">3</span>, b = (<span class="number">0x04</span> & x) >> <span class="number">2</span>, c = (<span class="number">0x02</span> & x) >> <span class="number">1</span>;<span class="comment">//取出后4位的前3位</span></span><br><span class="line"> <span class="type">int</span> flag_2 = !( (a & b) | (a & c) );</span><br><span class="line"> <span class="keyword">return</span> flag_1 & flag_2;</span><br><span class="line">}</span><br><span class="line"><span class="comment">//通过了,还蛮容易,不过运算符刚好用了15个,试试能不能再简化简化</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//看到了一个思路,说是根据上下界来判断,可以之后思考一下</span></span><br></pre></td></tr></table></figure>
<h2 id="conditional"><a href="#conditional" class="headerlink" title="conditional"></a>conditional</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> * conditional - same as x ? y : z </span></span><br><span class="line"><span class="comment"> * Example: conditional(2,4,5) = 4</span></span><br><span class="line"><span class="comment"> * Legal ops: ! ~ & ^ | + << >></span></span><br><span class="line"><span class="comment"> * Max ops: 16</span></span><br><span class="line"><span class="comment"> * Rating: 3</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="comment">//判定逻辑就是,x是否非0,如果非0,输出y;如果是0,输出z。</span></span><br><span class="line"><span class="comment">//想到了一个,我们只需要最后输出统一格式:mut + y</span></span><br><span class="line"><span class="comment">//然后中途修改mut即可</span></span><br><span class="line"><span class="comment">//mut满足:当x=0时,mut=z-y;当x!=0时,mut=0</span></span><br><span class="line"><span class="comment">//下面就是这个逻辑的实现,还蛮有意思的,通过0和非0的映射</span></span><br><span class="line"><span class="comment">//其实思路就是,我们直接用0作为选择器来生成mut变量,反正mut变量要么是z-y要么是0</span></span><br><span class="line"><span class="comment">//怎么选择呢?用或呗:00000和11111,就可以实现选择了</span></span><br><span class="line"><span class="comment">//比如,当x=0时,我们将门设为111...111,当x!=0时,我们将门设为000...000;</span></span><br><span class="line"><span class="comment">//然后将门与z-y或一下就行</span></span><br><span class="line"><span class="comment">//怎么从0和非0生成111...111和000...000,这个更简单了,000...000和111...111之间其实就差1,0和1也差1,你懂我意思吧</span></span><br><span class="line"><span class="comment">//看代码:</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">conditional</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y, <span class="type">int</span> z)</span> {</span><br><span class="line"> <span class="type">int</span> mut = ( ( ( ! ! (<span class="type">unsigned</span>)x ) + ~<span class="number">1</span> + <span class="number">1</span> ) ) & (z + ~y + <span class="number">1</span>);</span><br><span class="line"> <span class="keyword">return</span> mut + y;</span><br><span class="line">}</span><br><span class="line"><span class="comment">//通过了~</span></span><br></pre></td></tr></table></figure>
<h2 id="isLessOrEqual"><a href="#isLessOrEqual" class="headerlink" title="isLessOrEqual"></a>isLessOrEqual</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> * isLessOrEqual - if x <= y then return 1, else return 0 </span></span><br><span class="line"><span class="comment"> * Example: isLessOrEqual(4,5) = 1.</span></span><br><span class="line"><span class="comment"> * Legal ops: ! ~ & ^ | + << >></span></span><br><span class="line"><span class="comment"> * Max ops: 24</span></span><br><span class="line"><span class="comment"> * Rating: 3</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="comment">//相减,按照符号位进行判断输出,可以用上一题的结论</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">isLessOrEqual</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span> {</span><br><span class="line"> <span class="keyword">return</span> ( ( ( ! ! ((y + ~x + <span class="number">1</span>) >> <span class="number">31</span>) ) + ~<span class="number">0</span>) ) & <span class="number">1</span>;<span class="comment">//稍微化简一下</span></span><br><span class="line">}</span><br><span class="line"><span class="comment">//事实上通过不了,我们没考虑溢出</span></span><br><span class="line"><span class="comment">//那咋办,考虑一下呗</span></span><br><span class="line"><span class="comment">//思考一下,溢出有两种情况:x正y负,x负y正,前者溢出结果为正,后者溢出结果为负</span></span><br><span class="line"><span class="comment">//那么我们直接对于所有x负y正输出1,x正y负输出0?其余的正常判断</span></span><br><span class="line"><span class="comment">//那么需要两次判断,可以通过&和|实现01的优先条件判断</span></span><br><span class="line"><span class="comment">//比如,flag_np标记x负y正,flag_pn反之</span></span><br><span class="line"><span class="comment">//那么,当flag_np=1时,我们直接输出1;flag_pn=1时,直接输出0</span></span><br><span class="line"><span class="comment">//flag_np=0和flag_pn=0时我们就不管了</span></span><br><span class="line"><span class="comment">//假设原输出为output</span></span><br><span class="line"><span class="comment">//修正后的输出为:(2 + ~flag_pn) & (flag_np | output)</span></span><br><span class="line"><span class="comment">//逻辑是这样的:</span></span><br><span class="line"><span class="comment">//如果flag_pn=1,那么前面就爆0,直接输出0;如果flag_pn=0,进入后面的判断条件</span></span><br><span class="line"><span class="comment">//如果flag_np=1,那么直接输出1,否则输出output</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">isLessOrEqual</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span> {</span><br><span class="line"> <span class="type">int</span> xh = ((<span class="type">unsigned</span>)x) >> <span class="number">31</span>;</span><br><span class="line"> <span class="type">int</span> yh = ((<span class="type">unsigned</span>)y) >> <span class="number">31</span>;</span><br><span class="line"> <span class="type">int</span> flag_pn = !xh & yh;</span><br><span class="line"> <span class="type">int</span> flag_np = !yh & xh;</span><br><span class="line"> <span class="keyword">return</span> (<span class="number">2</span> + ~flag_pn) & (flag_np | ( ( ( ( ! ! ((y + ~x + <span class="number">1</span>) >> <span class="number">31</span>) ) + ~<span class="number">0</span>) ) & <span class="number">1</span> ) );</span><br><span class="line">}</span><br><span class="line"><span class="comment">//然后终于通过了...好耶!</span></span><br></pre></td></tr></table></figure>
</div>
<footer class="article-footer">
<a data-url="https://harryis666.github.io/2024/09/23/CASPP-Lab-%E7%AC%AC%E4%B8%80%E7%AB%A0%E9%A2%98%E8%A7%A3-1/" data-id="cm1ezf9xm0000psu46iog313q" data-title="CASPP Lab 第一章题解(1)" class="article-share-link"><span class="fa fa-share">Share</span></a>
<ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/CSAPP/" rel="tag">CSAPP</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E9%A2%98%E8%A7%A3/" rel="tag">题解</a></li></ul>
</footer>
</div>
</article>
<article id="post-hello-world" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
<div class="article-meta">
<a href="/2024/09/23/hello-world/" class="article-date">
<time class="dt-published" datetime="2024-09-23T11:44:33.938Z" itemprop="datePublished">2024-09-23</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="p-name article-title" href="/2024/09/23/hello-world/">Hello World</a>
</h1>
</header>
<div class="e-content article-entry" itemprop="articleBody">
<p>Welcome to <a target="_blank" rel="noopener" href="https://hexo.io/">Hexo</a>! This is your very first post. Check <a target="_blank" rel="noopener" href="https://hexo.io/docs/">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a target="_blank" rel="noopener" href="https://hexo.io/docs/troubleshooting.html">troubleshooting</a> or you can ask me on <a target="_blank" rel="noopener" href="https://github.com/hexojs/hexo/issues">GitHub</a>.</p>
<h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo new <span class="string">"My New Post"</span></span><br></pre></td></tr></table></figure>
<p>More info: <a target="_blank" rel="noopener" href="https://hexo.io/docs/writing.html">Writing</a></p>
<h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo server</span><br></pre></td></tr></table></figure>
<p>More info: <a target="_blank" rel="noopener" href="https://hexo.io/docs/server.html">Server</a></p>
<h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo generate</span><br></pre></td></tr></table></figure>
<p>More info: <a target="_blank" rel="noopener" href="https://hexo.io/docs/generating.html">Generating</a></p>
<h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo deploy</span><br></pre></td></tr></table></figure>
<p>More info: <a target="_blank" rel="noopener" href="https://hexo.io/docs/one-command-deployment.html">Deployment</a></p>
</div>
<footer class="article-footer">
<a data-url="https://harryis666.github.io/2024/09/23/hello-world/" data-id="cm1ey1lp90000jgu4cak251k8" data-title="Hello World" class="article-share-link"><span class="fa fa-share">Share</span></a>
</footer>
</div>
</article>
</section>
<aside id="sidebar">
<div class="widget-wrap">
<h3 class="widget-title">Categories</h3>
<div class="widget">
<ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/CSAPP/">CSAPP</a></li></ul>
</div>
</div>
<div class="widget-wrap">
<h3 class="widget-title">Tags</h3>
<div class="widget">
<ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/CSAPP/" rel="tag">CSAPP</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E9%A2%98%E8%A7%A3/" rel="tag">题解</a></li></ul>
</div>
</div>
<div class="widget-wrap">
<h3 class="widget-title">Tag Cloud</h3>
<div class="widget tagcloud">
<a href="/tags/CSAPP/" style="font-size: 10px;">CSAPP</a> <a href="/tags/%E9%A2%98%E8%A7%A3/" style="font-size: 10px;">题解</a>
</div>
</div>
<div class="widget-wrap">
<h3 class="widget-title">Archives</h3>
<div class="widget">
<ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2024/09/">September 2024</a></li></ul>
</div>
</div>
<div class="widget-wrap">
<h3 class="widget-title">Recent Posts</h3>
<div class="widget">
<ul>
<li>
<a href="/2024/09/23/CASPP-Lab-%E7%AC%AC%E4%B8%80%E7%AB%A0%E9%A2%98%E8%A7%A3-1/">CASPP Lab 第一章题解(1)</a>
</li>
<li>
<a href="/2024/09/23/hello-world/">Hello World</a>
</li>
</ul>
</div>
</div>
</aside>
</div>
<footer id="footer">
<div class="outer">
<div id="footer-info" class="inner">
© 2024 Harry Li<br>
Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
</div>
</div>
</footer>
</div>
<nav id="mobile-nav">
<a href="/" class="mobile-nav-link">Home</a>
<a href="/archives" class="mobile-nav-link">Archives</a>
</nav>
<script src="/js/jquery-3.6.4.min.js"></script>
<script src="/fancybox/jquery.fancybox.min.js"></script>
<script src="/js/script.js"></script>
</div>
</body>
</html>