重新用C负数presentation? [英] Representation of negative numbers in C?

查看:162
本文介绍了重新用C负数presentation?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何c重新present负整数?

How does C represent negative integers?

它是由二的补码重新presentation或通过使用MSB(最高显著位)

Is it by two's complement representation or by using the MSB (most significant bit)?

1 以十六进制为 FFFFFFFF

所以,请澄清这对我来说。

So please clarify this for me.

推荐答案

ISO C(C99),截面 6.2.6.2/2 ,指出实现必须选一个三种不同的再presentations积分的数据类型,2的补一补数或符号/幅度(尽管它是令人难以置信的可能是两个互补的实现远远大于其他)。

ISO C (C99), section 6.2.6.2/2, states that an implementation must choose one of three different representations for integral data types, two's complement, ones' complement or sign/magnitude (although it's incredibly likely that the two's complement implementations far outweigh the others).

在所有这些重新presentations,正数是相同的,唯一的区别是负数。

In all those representations, positive numbers are identical, the only difference being the negative numbers.

要获得负重presentation为正数,您可以:

To get the negative representation for a positive number, you:


  • 反转所有位,然后添加一个二进制补码。

  • 反转所有位为一补数。

  • 转化在符号位为符号/幅度。

您可以在下面的表格中看到这一点:

You can see this in the table below:


number | two's complement    | ones' complement    | sign/magnitude
=======|=====================|=====================|====================
     5 | 0000 0000 0000 0101 | 0000 0000 0000 0101 | 0000 0000 0000 0101
    -5 | 1111 1111 1111 1011 | 1111 1111 1111 1010 | 1000 0000 0000 0101

请即ISO并不强制所有位都在重新presentation使用。他们引进了符号位的概念,价值位的和填充位。的现在,我从来没有真正的看到的与填充比特的实现,但是,从C99理由文件,他们有这样的解释:

Keep in mind that ISO doesn't mandate that all bits are used in the representation. They introduce the concept of a sign bit, value bits and padding bits. Now I've never actually seen an implementation with padding bits but, from the C99 rationale document, they have this explanation:

假设一台机器使用一对16位短裤(每个都有自己的符号位)来弥补一个32位int和下短的符号位在这32位int使用时被忽略。然后,作为一个32位有符号整数,存在在确定的32位有符号整数的值20忽略的填充位(在32位的中间)。但是,如果该32位项目作为一个32位无符号整型处理,然后该填充位是给用户的程序可见。的C委员会被告知有这种方式工作的一台机器,这是一个原因是填充比特添加到C99。

Suppose a machine uses a pair of 16-bit shorts (each with its own sign bit) to make up a 32-bit int and the sign bit of the lower short is ignored when used in this 32-bit int. Then, as a 32-bit signed int, there is a padding bit (in the middle of the 32 bits) that is ignored in determining the value 20 of the 32-bit signed int. But, if this 32-bit item is treated as a 32-bit unsigned int, then that padding bit is visible to the user’s program. The C committee was told that there is a machine that works this way, and that is one reason that padding bits were added to C99.

这篇关于重新用C负数presentation?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆