首页 新闻 搜索 专区 学院

位运算(右移) <<的疑问

0
悬赏园豆:5 [已解决问题] 解决于 2018-02-07 12:10
/*
对一个二进制位移,一般什么场景让位移功能存在?
右移有正负数字判断,对二进制数还有正负? 难道-0010?

给个右移的直观教程,动画is wonderful...

谢了。
*/
问题补充:

符号知道了,   1000 0010  (负数)  0000 1001 (正数)

Coca-code的主页 Coca-code | 初学一级 | 园豆:9
提问于:2018-02-07 10:28
< >
分享
最佳答案
0
收获园豆:5
dudu | 高人七级 |园豆:39014 | 2018-02-07 11:27

谢了,了解一下。。

Coca-code | 园豆:9 (初学一级) | 2018-02-07 12:11
其他回答(1)
0

比如 int 转 byte之类的。

当然也可以直接运算,比如对于整形来说左移1大约就是*2,右移1大约就是/2。

例如参见BinaryWriter(下面的函数是扩展了,去掉Big就是原有的,它里面的实现就有移位):

protected override void OnWriteToBuffer(BinaryWriter writer)
{
writer.WriterBig(Length);
writer.WriterBig(Number);
writer.WriterBig((ushort)Catelog);
writer.WriterBig(SessionId);
writer.WriterBig(Version.Buffer);
writer.WriterBig((byte)EncryptOpition);
writer.WriterBig(KeyForCommonEncript);
}

花飘水流兮 | 园豆:11000 (专家六级) | 2018-02-07 11:41

谢了,

我是想:大多数算法,都是对10进制数字的操作, 利用二进制5种运算特性,或许可以给算法开辟另一番蓝海。。

支持(0) 反对(0) Coca-code | 园豆:9 (初学一级) | 2018-02-07 12:10

@Coca-code: 呵呵。有想象力很好,你那么有兴趣应该好好看看计算机原理,看看布尔数学。

支持(0) 反对(0) 花飘水流兮 | 园豆:11000 (专家六级) | 2018-02-07 12:51

@花飘水流兮: 恩师

支持(0) 反对(0) Coca-code | 园豆:9 (初学一级) | 2018-02-07 13:00
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
                    >
    <!--  应该在此定义资源字典条目。  -->
    <BitmapImage x:Key="LogoImage" UriSource="/WpfApp1;component/Image/Logo/Logo.png" />

    <!--  Close Resources。  -->
    <BitmapImage x:Key="CloseNormalImage" UriSource="/WpfApp1;component/Image/Close/Close-Normal.png" />
    <BitmapImage x:Key="CloseIsMouseOverImage" UriSource="/WpfApp1;component/Image/Maximize/Maximize-IsMouseOver.png" />
    <BitmapImage x:Key="CloseIsPressedImage" UriSource="/WpfApp1;component/Image/Close/Close-IsPressed.png" />

    <!--  Minimize Resources。  -->
    <BitmapImage x:Key="MinimizeNormalImage" UriSource="/WpfApp1;component/Image/Maximize/Minimize-Normal.png" />
    <BitmapImage x:Key="MinimizeIsMouseOverImage" UriSource="/WpfApp1;component/Image/Maximize/Minimize-IsMouseOver.png" />
    <BitmapImage x:Key="MinimizeIsPressedImage" UriSource="/WpfApp1;component/Image/Minimize/Minimize-IsPressed.png" />

    <!--  Maximise Resources。  -->
    <BitmapImage x:Key="MaximizeNormalImage" UriSource="/WpfApp1;component/Image/Maximize/Maximize-Normal.png" />
    <BitmapImage x:Key="MaximizeIsMouseOverImage" UriSource="/WpfApp1;component/Image/Maximize/Maximize-IsMouseOver.png" />
    <BitmapImage x:Key="MaximizeIsPressedImage" UriSource="/WpfApp1;component/Image/Maximize/Maximize-IsPressed.png" />

    <LinearGradientBrush x:Key="ButtonCommonBrush" StartPoint="0.5,0" EndPoint="0.5,1">
        <GradientStop Offset="0" Color="#FFC7E4FE" />
        <GradientStop Offset="0.5" Color="#FFC7E4FE" />
        <GradientStop Offset="0.5" Color="#FFA2D8F8" />
        <GradientStop Offset="0.55" Color="#FFA2D8F8" />
        <GradientStop Offset="0.55" Color="#FF8DD5F6" />
        <GradientStop Offset="1" Color="#FF8DD5F6" />
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="ButtonPressedBrush" StartPoint="0.5,0" EndPoint="0.5,1">
        <GradientStop Offset="0" Color="#FF8DC7FC" />
        <GradientStop Offset="0.5" Color="#FF8DC7FC" />
        <GradientStop Offset="0.5" Color="#FF6CB1F1" />
        <GradientStop Offset="0.55" Color="#FF6CB1F1" />
        <GradientStop Offset="0.55" Color="#FF54A4EA" />
        <GradientStop Offset="1" Color="#FF54A4EA" />
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="ButtonNormalBackground" StartPoint="0,0" EndPoint="0,1">
        <GradientStop Offset="0" Color="#FFE5E5E5" />
        <GradientStop Offset="0.5" Color="#EBEBEB" />
        <GradientStop Offset="0.5" Color="#FFCACACA" />
        <GradientStop Offset="1" Color="DarkGray" />
    </LinearGradientBrush>
    <SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070" />
    <SolidColorBrush x:Key="ButtonMouseOverBorder" Color="#FF5FAFEE" />
    <SolidColorBrush x:Key="ButtonPressedBorderBrush" Color="#FF7BCB48" />
    <Style TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <Border x:Name="border"
                                Background="{DynamicResource ButtonNormalBackground}"
                                BorderBrush="{DynamicResource ButtonNormalBorder}"
                                BorderThickness="1"
                                CornerRadius="6" />
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          RecognizesAccessKey="True"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True" />
                        <Trigger Property="IsDefaulted" Value="True" />
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="border" Property="Background" Value="{DynamicResource ButtonCommonBrush}" />
                            <Setter TargetName="border" Property="BorderBrush" Value="{DynamicResource ButtonMouseOverBorder}" />
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter TargetName="border" Property="Background" Value="{DynamicResource ButtonPressedBrush}" />
                            <Setter TargetName="border" Property="BorderBrush" Value="{DynamicResource ButtonPressedBorderBrush}" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False" />
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="TabControlButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <Border x:Name="border"
                                Background="{DynamicResource ButtonNormalBackground}"
                                BorderBrush="{DynamicResource ButtonNormalBorder}"
                                BorderThickness="1"
                                CornerRadius="6" />
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          RecognizesAccessKey="True"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True" />
                        <Trigger Property="IsDefaulted" Value="True" />
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="border" Property="Background" Value="{DynamicResource ButtonCommonBrush}" />
                            <Setter TargetName="border" Property="BorderBrush" Value="{DynamicResource ButtonMouseOverBorder}" />
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter TargetName="border" Property="Background" Value="{DynamicResource ButtonPressedBrush}" />
                            <Setter TargetName="border" Property="BorderBrush" Value="{DynamicResource ButtonPressedBorderBrush}" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False" />
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="CloseButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Image Name="image" Source="{StaticResource CloseNormalImage}" />
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="image" Property="Source" Value="{StaticResource CloseIsMouseOverImage}" />
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter TargetName="image" Property="Source" Value="{StaticResource CloseIsPressedImage}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="MinimizeButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Image Name="image" Source="{StaticResource MinimizeNormalImage}" />
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="image" Property="Source" Value="{StaticResource MinimizeIsMouseOverImage}" />
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter TargetName="image" Property="Source" Value="{StaticResource MinimizeIsPressedImage}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="MaximizeButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Image Name="image" Source="{StaticResource MaximizeNormalImage}" />
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="image" Property="Source" Value="{StaticResource MaximizeIsMouseOverImage}" />
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter TargetName="image" Property="Source" Value="{StaticResource MaximizeIsPressedImage}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
支持(0) 反对(0) 大da脸 | 园豆:136 (初学一级) | 2018-02-08 10:11
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册