位移算术解释(C)

时间:2017-02-11 01:37:35

标签: c

我有一个家庭作业问题,我知道答案,但不明白为什么它是答案,只是希望找到一个可以向我解释的人。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Master.aspx.cs" Inherits="HarnessMaintain.Pages.Master" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:CommandField ShowSelectButton="True" />
                <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="MODE_TYPE" HeaderText="MODE_TYPE" SortExpression="MODE_TYPE" />
                <asp:BoundField DataField="PARTNER_NAME" HeaderText="PARTNER_NAME" SortExpression="PARTNER_NAME" />
            </Columns>
        </asp:GridView>
        <br />
        <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" DataSourceID="SqlDataSource2"
            OnItemDeleted="DetailsView1_ItemDeleted" OnItemInserted="DetailsView1_ItemInserted"
            OnItemUpdated="DetailsView1_ItemUpdated">
            <Fields>
                <asp:CommandField ShowDeleteButton="True" />
            </Fields>
        </asp:DetailsView>
        <br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT &quot;ID&quot;, &quot;MODE_TYPE&quot;, &quot;PARTNER_NAME&quot; FROM &quot;MSWSCEMASTER&quot; ORDER BY &quot;ID&quot;">
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            DeleteCommand="DELETE FROM &quot;MSWSCEMASTER&quot; WHERE &quot;ID&quot; =:ID"
            ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM &quot;MSWSCEMASTER&quot; WHERE (&quot;ID&quot; = :ID)">
            <DeleteParameters>
                <asp:Parameter Name=":ID" Type="Decimal" />
            </DeleteParameters>
            <SelectParameters>
                <asp:ControlParameter ControlID="GridView1" Name="ID" PropertyName="SelectedValue"
                    Type="Decimal" />
            </SelectParameters>
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

所以答案就出来了,所以C =&#39; 0&#39;。我认为&#39; a&lt;&lt; B&#39;将等于0,&#39; b&lt;&lt;一个&#39;会出现1,所以&#39; c = 0 + 1&#39;。为什么这是错的?

然而,当我尝试时:

#include <stdio.h>

int main(void){
    int a = 0, b = 1, c;
    c = a << b + b << a;

    printf("d%\n",c);
    return 0;
}

C等于&#39; 2&#39;。这对我来说更没意义。如果有人可以向我解释为什么它会像这样工作,或者指出我向我澄清这种行为的一些参考,我将非常感激。

2 个答案:

答案 0 :(得分:3)

语言语法(或者#34;运算符优先级&#34;,如果你愿意的话)说

c = a << b + b << a;

实际上是

c = ((a << (b + b)) << a);

此表达式在a的值附近移动而没有别的。由于a的值为0,因此结果除了0之外别无其他。

相同的分组告诉你

c = b << a + a << b;

c = ((b << (a + a)) << b);

c = ((1 << 0) << 1);

2来自哪里。

答案 1 :(得分:1)

操作顺序。运算符<<的优先级低于运算符+,因此:

  • 0 << 1 + 1 << 00 << (1 + 1) << 00 << 2 << 0当然是0

  • 1 << 0 + 1 << 01 << (0 + 1) << 01 << 1 << 0当然是2

如有疑问,请务必使用括号,即使没有疑问。