热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

带符号整数的算术位移。-Arithmeticbit-shiftonasignedinteger

Iamtryingtofigureouthowexactlyarithmeticbit-shiftoperatorsworkinC,andhowitwillaffe

I am trying to figure out how exactly arithmetic bit-shift operators work in C, and how it will affect signed 32-bit integers.

我正在试图弄清楚算术位移位操作符在C中是如何工作的,以及它将如何影响已签名的32位整数。

To make things simple, let's say we work within one byte (8 bits):

为了简单起见,假设我们在一个字节(8比特)内工作:

x = 1101.0101
MSB[ 1101.0101 ]LSB

Reading other posts on Stack Overflow and some websites, I found that: << will shift toward MSB (to the left, in my case), and fill "empty" LSB bits with 0s.

在Stack Overflow和一些网站上阅读其他文章,我发现:<<将向msb移动(在我的情况下),并填充“空”lsb位与0。

And >> will shift toward LSB (to the right, in my case) and fill "empty" bits with MS bit

>>将向LSB(在我的情况下)向右移动,并将“空”位填充到MS比特。

So, x = x <<7 will result in moving LSB to MSB, and setting everything to 0s.

因此,x = x <<7将会使LSB移动到MSB,并将一切设置为0。

1000.0000

Now, let's say I would >> 7, last result. This would result in [0000.0010]? Am I right?

现在,假设我要>> 7,最后一个结果。这将导致[0000.0010]?我说的对吗?

Am I right about my assumptions about shift operators?

我对移动运营商的假设是否正确?

I just tested on my machine, **

我刚在我的机器上测试过。

int x = 1;   //000000000......01

x = x <<31; //100000000......00

x = x >> 31; //111111111......11 (Everything is filled with 1s !!!!!) 

Why?

为什么?

4 个解决方案

#1


47  

Right shift of a negative signed number has implementation-defined behaviour.

负签名数的右移具有实现定义的行为。

If your 8 bits are meant to represent a signed 8 bit value (as you're talking about a "signed 32 bit integer" before switching to 8 bit examples) then you have a negative number. Shifting it right may fill "empty" bits with the original MSB (i.e. perform sign extension) or it may shift in zeroes, depending on platform and/or compiler.

如果你的8位代表一个有符号的8位值(在切换到8位示例之前,你正在讨论一个“有符号的32位整数”),那么你就有一个负数。将其右移可能会填满“空”位与原始的MSB(即执行符号扩展),或者它可以在零中移动,这取决于平台和/或编译器。

(Implementation-defined behaviour means that the compiler will do something sensible, but in a platform-dependent manner; the compiler documentation is supposed to tell you what.)

(实现定义的行为意味着编译器会做一些有意义的事情,但是是以平台依赖的方式;编译器文档应该告诉你什么。


A left shift, if the number either starts out negative, or the shift operation would shift a 1 either to or beyond the sign bit, has undefined behaviour (as do most operations on signed values which cause an overflow).

一个左移,如果这个数字开始是负数,或者移位操作会将一个1移动到符号位或者超过符号位,就会有未定义的行为(对于导致溢出的符号值的大多数操作也是如此)。

(Undefined behaviour means that anything at all could happen.)

(未定义的行为意味着任何事情都可能发生。)


The same operations on unsigned values are well-defined in both cases: the "empty" bits will be filled with 0.

在两种情况下,对未签名值的相同操作都是定义良好的:“空”位将填充为0。

#2


32  

Bitwise shift operations are not defined for negative values

位移位操作不是为负值定义的。

for '<<'

“<<”

6.5.7/4 [...] If E1 has a signed type and nonnegative value, and E1×2E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.

6.5.7/4[…)如果E1签署类型和非负价值,在结果和E1×2 e2表示的类型,那么得到的值;否则,行为是未定义的。

and for '>>'

和“> >”

6.5.7/5 [...] If E1 has a signed type and a negative value, the resulting value is implementation- defined.

6.5.7/5[…]如果E1有一个符号类型和一个负值,则生成的值是实现定义的。

It's a waste of time to study the behaviour of these operations on signed numbers on a specific implementation, because you have no guarantee it will work the same way on any other implementation (an implementation is, for example, you compiler on your computer with your specific commad-line parameters).

研究这些操作在特定实现上的签名数字的行为是浪费时间,因为您不能保证它在任何其他实现上的工作方式都是相同的(例如,您的计算机上的编译器会使用特定的逗号行参数)。

It might not even work for an older or a newer version of the very same compiler. The compiler might even define those bits as random or undefined. This would mean that the very same code sequence could produce totally different results when used across your sources or even depend on things like assembly optimisation or other register usage. If encapsulated in a function it might not even produce the same result in those bits on two consecutive calls with the same arguments.

它甚至可能不会为更老的版本或同一编译器的新版本工作。编译器甚至可以将这些位定义为随机的或未定义的。这意味着,当使用不同的源代码时,相同的代码序列可能产生完全不同的结果,甚至依赖于诸如组装优化或其他寄存器使用之类的东西。如果封装在一个函数中,它甚至可能不会在两个连续调用相同参数的情况下产生相同的结果。

Considering only non-negative values, the effect of left shifting by 1 (expression <<1) is the same as multpliying the expression by 2 (provided expression * 2 does not overflow) and the effect of right shifting by 1 (expression >> 1) is the same as dividing by 2.

仅考虑非负值的值,左移1(表达式<<1)的效果与2(提供的表达式* 2不溢出)和右移1(表达式>> 1)的效果相同,即除以2。

#3


5  

As others said shift of negative value is implementation-defined.

正如其他人所说的,负值的转移是由实现定义的。

Most of implementations treat signed right shift as floor(x/2N) by filling shifted in bits using sign bit. It is very convenient in practice, as this operation is so common. On the other hand if you will shift right unsigned integer, shifted in bits will be zeroed.

大多数实现将签名的右移作为层(x/2N),用符号位填充移位。这在实践中非常方便,因为这个操作非常普遍。另一方面,如果你转换右无符号整数,移位的位将是零。

Looking from machine side, most implementations have two types of shift-right instructions:

从机器的角度来看,大多数实现都有两种类型的转换正确的指令:

  1. An 'arithmetic' shift right (often having mnemonic ASR or SRA) which works as me explained.

    一种“算术”的右移(通常有助记符ASR或SRA),正如我所解释的那样。

  2. A 'logic' shift right (oftem having mnemonic LSR or SRL or SR) which works as you expect.

    一个“逻辑”的右移(oftem有助记符LSR或SRL或SR),这是你所期望的。

Most of compilers utilize first for signed types and second for unsigned ones. Just for convenience.

大多数编译器首先使用已签名的类型,其次使用未签名的类型。只是为了方便。

#4


0  

In the 32 bit compiler

在32位编译器中。

x = x >> 31;

x = x >> 31;

here x is the signed integer so 32nd bit is sign bit.

这里x是有符号整数,所以32位是符号位。

final x value is 100000...000. and 32nd bit indicate -ive value.

最后的x值是100000…000。第32位表示-ive值。

here x value implement to 1's compliment.

这里x值执行到1的恭维。

then final x is -32768

最后x是-32768。


推荐阅读
  • [线段树|平衡树|树状数组]LightOJ - 1087 - Diablo
    1087-DiabloPDF(English)StatisticsForum ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 加密世界下一个主流叙事领域:L2、跨链桥、GameFi等
    本文介绍了加密世界下一个主流叙事的七个潜力领域,包括L2、跨链桥、GameFi等。L2作为以太坊的二层解决方案,在过去一年取得了巨大成功,跨链桥和互操作性是多链Web3中最重要的因素。去中心化的数据存储领域也具有巨大潜力,未来云存储市场有望达到1500亿美元。DAO和社交代币将成为购买和控制现实世界资产的重要方式,而GameFi作为数字资产在高收入游戏中的应用有望推动数字资产走向主流。衍生品市场也在不断发展壮大。 ... [详细]
  • 本文介绍了Windows操作系统的版本及其特点,包括Windows 7系统的6个版本:Starter、Home Basic、Home Premium、Professional、Enterprise、Ultimate。Windows操作系统是微软公司研发的一套操作系统,具有人机操作性优异、支持的应用软件较多、对硬件支持良好等优点。Windows 7 Starter是功能最少的版本,缺乏Aero特效功能,没有64位支持,最初设计不能同时运行三个以上应用程序。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • Android自定义控件绘图篇之Paint函数大汇总
    本文介绍了Android自定义控件绘图篇中的Paint函数大汇总,包括重置画笔、设置颜色、设置透明度、设置样式、设置宽度、设置抗锯齿等功能。通过学习这些函数,可以更好地掌握Paint的用法。 ... [详细]
  • 颜色迁移(reinhard VS welsh)
    不要谈什么天分,运气,你需要的是一个截稿日,以及一个不交稿就能打爆你狗头的人,然后你就会被自己的才华吓到。------ ... [详细]
  • Introduction(简介)Forbeingapowerfulobject-orientedprogramminglanguage,Cisuseda ... [详细]
  • 在本教程中,我们将看到如何使用FLASK制作第一个用于机器学习模型的RESTAPI。我们将从创建机器学习模型开始。然后,我们将看到使用Flask创建AP ... [详细]
  • [翻译]PyCairo指南裁剪和masking
    裁剪和masking在PyCairo指南的这个部分,我么将讨论裁剪和masking操作。裁剪裁剪就是将图形的绘制限定在一定的区域内。这样做有一些效率的因素࿰ ... [详细]
  • Answer:Theterm“backslash”isonofthemostincorrectlyusedtermsincomputing.People ... [详细]
  • 用户视图(查看运行状态或其他参数)系统视图(配置设备的系统参数)system-viewEntersystemview,returnuservi ... [详细]
  • 数学和统计方法sum对数组中全部或某轴向的元素求和。零长度的数组的sum为0。mean算术平均数。零长度的数组的mean为NaN。importnumpyas ... [详细]
author-avatar
dmcm0005
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有