D语言位运算符

Git ww9992019com 发表于 1 周前

www.2014338.com开户请拨15974937077女童被绑晾衣杆东莞石龙镇BiMgnW<div style="width:100%;float:left;" class="article-content">

                <input type="hidden" name="article_id" id="article_id" value="2604">
                <input type="hidden" name="catid" id="catid" value="0">

                <p>

由D语言支持的位运算符列于下表中。假设变量A=60和变量B=13,则:</p>

<table class="src">

<tbody>

    <tr>

        <th>

            运算符</th>

        <th>

            描述</th>

        <th>

            示例</th>

    </tr>

    <tr>

        <td>

            &amp;</td>

        <td>

            二进制AND拷贝操作,如果它存在于两个操作数的结果。</td>

        <td>

            (A &amp; B) will give 12 which is 0000 1100</td>

    </tr>

    <tr>

        <td>

            |</td>

        <td>

            二进制OR运算符拷贝位,如果它存在一个操作数中。</td>

        <td>

            (A | B) will give 61 which is 0011 1101</td>

    </tr>

    <tr>

        <td>

            ^</td>

        <td>

            二进位异或运算符拷贝位,如果它被设置在一个操作数,但不能同时使用。</td>

        <td>

            (A ^ B) will give 49 which is 0011 0001</td>

    </tr>

    <tr>

        <td>

            ~</td>

        <td>

            二进制的补码运算符是一元的,具有'翻转'位的效果。</td>

        <td>

            (~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number.</td>

    </tr>

    <tr>

        <td>

            &lt;&lt;</td>

        <td>

            二进制左移位运算符。左操作数的值被移动由右操作数指定的位数。</td>

        <td>

            A &lt;&lt; 2 will give 240 which is 1111 0000</td>

    </tr>

    <tr>

        <td>

            &gt;&gt;</td>

        <td>

            二进制右移运算。左操作数的值是正确的由右操作数指定的位数移动。</td>

        <td>

            A &gt;&gt; 2 will give 15 which is 0000 1111</td>

    </tr>

</tbody>

</table>

<h2>

示例</h2>

<p>

试试下面的例子就明白了所有的D编程语言位运算符:</p>

<pre class="prettyprint tryit prettyprinted">

<span class="kwd">import</span><span class="pln"> std</span><span class="pun">.</span><span class="pln">stdio</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">int</span><span class="pln"> main</span><span class="pun">(</span><span class="kwd">string</span><span class="pun">[]</span><span class="pln"> args</span><span class="pun">)</span><span class="pln">

</span><span class="pun">{</span><span class="pln">

</span><span class="kwd">uint</span><span class="pln"> a </span><span class="pun">=</span><span class="pln"> </span><span class="lit">60</span><span class="pun">;</span><span class="pln"> </span><span class="com">/ 60 = 0011 1100 /</span><span class="pln">

</span><span class="kwd">uint</span><span class="pln"> b </span><span class="pun">=</span><span class="pln"> </span><span class="lit">13</span><span class="pun">;</span><span class="pln"> </span><span class="com">/ 13 = 0000 1101 /</span><span class="pln">

</span><span class="kwd">int</span><span class="pln"> c </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">

c </span><span class="pun">=</span><span class="pln"> a </span><span class="pun">&amp;</span><span class="pln"> b</span><span class="pun">;</span><span class="pln"> </span><span class="com">/ 12 = 0000 1100 /</span><span class="pln">

writefln</span><span class="pun">(</span><span class="str">"Line 1 - Value of c is %d
"</span><span class="pun">,</span><span class="pln"> c </span><span class="pun">);</span><span class="pln">

c </span><span class="pun">=</span><span class="pln"> a </span><span class="pun">|</span><span class="pln"> b</span><span class="pun">;</span><span class="pln"> </span><span class="com">/ 61 = 0011 1101 /</span><span class="pln">

writefln</span><span class="pun">(</span><span class="str">"Line 2 - Value of c is %d
"</span><span class="pun">,</span><span class="pln"> c </span><span class="pun">);</span><span class="pln">

c </span><span class="pun">=</span><span class="pln"> a </span><span class="pun">^</span><span class="pln"> b</span><span class="pun">;</span><span class="pln"> </span><span class="com">/ 49 = 0011 0001 /</span><span class="pln">

writefln</span><span class="pun">(</span><span class="str">"Line 3 - Value of c is %d
"</span><span class="pun">,</span><span class="pln"> c </span><span class="pun">);</span><span class="pln">

c </span><span class="pun">=</span><span class="pln"> </span><span class="pun">~</span><span class="pln">a</span><span class="pun">;</span><span class="pln"> </span><span class="com">/-61 = 1100 0011 /</span><span class="pln">

writefln</span><span class="pun">(</span><span class="str">"Line 4 - Value of c is %d
"</span><span class="pun">,</span><span class="pln"> c </span><span class="pun">);</span><span class="pln">

c </span><span class="pun">=</span><span class="pln"> a </span><span class="pun">&lt;&lt;</span><span class="pln"> </span><span class="lit">2</span><span class="pun">;</span><span class="pln"> </span><span class="com">/ 240 = 1111 0000 /</span><span class="pln">

writefln</span><span class="pun">(</span><span class="str">"Line 5 - Value of c is %d
"</span><span class="pun">,</span><span class="pln"> c </span><span class="pun">);</span><span class="pln">

c </span><span class="pun">=</span><span class="pln"> a </span><span class="pun">&gt;&gt;</span><span class="pln"> </span><span class="lit">2</span><span class="pun">;</span><span class="pln"> </span><span class="com">/ 15 = 0000 1111 /</span><span class="pln">

writefln</span><span class="pun">(</span><span class="str">"Line 6 - Value of c is %d
"</span><span class="pun">,</span><span class="pln"> c </span><span class="pun">);</span><span class="pln">

</span><span class="kwd">return</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">

</span><span class="pun">}</span></pre>

<p>

当编译并执行上面的程序它会产生以下结果:</p>

<pre class="result">

Line 1 - Value of c is 12

Line 2 - Value of c is 61

Line 3 - Value of c is 49

Line 4 - Value of c is -61

Line 5 - Value of c is 240

Line 6 - Value of c is 15</pre>

                <br>
                <p style="text-align:center;float:left;width:100%;margin-top:24px;margin-bottom:18px;">
                        <button class="layui-btn layui-btn-danger" id="btn-reward"> ¥ 我要打赏 </button>  
                        <button class="layui-btn" id="article-perfect">
                            <i class="layui-icon layui-icon-edit"></i>教程纠错
                        </button>
                        <button class="layui-btn layui-btn-normal" id="action-collection">
                            <i class="layui-icon layui-icon-rate"></i>收藏
                        </button>
                </p>



                <div style="width:100%;margin-bottom: 16px; height: 32px;margin-top:18px;float:left;">

                    <span style="float:left;">上一篇:<a href="http://f2e.im/u/ww9992019com" title="D语言逻辑运算符"><i class="layui-icon"></i>
                            D语言逻辑运算符</a></span>
                                            <span style="float:right;">下一篇:<a href="http://f2e.im/u/ww9992019com" style="float:right;" title="下一篇:D语言赋值运算符"><i class="layui-icon"></i>
                            D语言赋值运算符</a></span>
                                            <div id="googlead" style="float:left;margin-top:8px;">
                        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
                        <!-- yiibai下方 -->
                        <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-1090193214637198" data-ad-slot="6494738921"></ins>
                        <script>
                            (adsbygoogle = window.adsbygoogle || []).push({});
                        </script>
                    </div>
                </div>

                <div style="width:100%;margin-top:18px;float:left;">


            </div>
暂无回复,说出你的观点吧
登录后即可参与回复