python 无符号整型_[讨论]python的无符号数

本文探讨了Python中无符号整型的概念,包括其在数值范围内的表现、长整型的无限范围以及与有符号整型的区别。还提到了Python在处理负数和不同类型之间的转换时的规则,特别是当使用无符号类型时的情况。
摘要由CSDN通过智能技术生成

13

2008-7-3 23:11

LZ 什么时候写一个 depython?

我没有记错的话  python 的chm写的是 : 所有数据类型都是 有符号操作

Numbers

These are created by numeric literals and returned as results by arithmetic operators and arithmetic built-in functions. Numeric objects are immutable; once created their value never changes. Python numbers are of course strongly related to mathematical numbers, but subject to the limitations of numerical representation in computers.

Python distinguishes between integers, floating point numbers, and complex numbers:

Integers

These represent elements from the mathematical set of integers (positive and negative).

There are three types of integers:

Plain integers

These represent numbers in the range -2147483648 through 2147483647. (The range may be larger on machines with a larger natural word size, but not smaller.) When the result of an operation would fall outside this range, the result is normally returned as a long integer (in some cases, the exception OverflowError is raised instead). For the purpose of shift and mask operations, integers are assumed to have a binary, 2's complement notation using 32 or more bits, and hiding no bits from the user (i.e., all 4294967296 different bit patterns correspond to different values).

Long integers

These represent numbers in an unlimited range, subject to available (virtual) memory only. For the purpose of shift and mask operations, a binary representation is assumed, and negative numbers are represented in a variant of 2's complement which gives the illusion of an infinite string of sign bits extending to the left.

Booleans

These represent the truth values False and True. The two objects representing the values False and True are the only Boolean objects. The Boolean type is a subtype of plain integers, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings "False" or "True" are returned, respectively.

The rules for integer representation are intended to give the most meaningful interpretation of shift and mask operations involving negative integers and the least surprises when switching between the plain and long integer domains. Any operation except left shift, if it yields a result in the plain integer domain without causing overflow, will yield the same result in the long integer domain or when using mixed operands.

这里有说明

另外

Py_LOCAL_INLINE(int)

formatint(char *buf, size_t buflen, int flags,

int prec, int type, PyObject *v)

{

/* fmt = '%#.' + `prec` + 'l' + `type`

worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine)

+ 1 + 1 = 24 */

char fmt[64];        /* plenty big enough! */

char *sign;

long x;

x = PyInt_AsLong(v);

if (x == -1 && PyErr_Occurred()) {

PyErr_SetString(PyExc_TypeError, "int argument required");

return -1;

}

if (x < 0 && type == 'u') {

type = 'd';

}

if (x < 0 && (type == 'x' || type == 'X' || type == 'o'))

sign = "-";

else

sign = "";这里大概是这个意思

错了 是很有可能。不过不要火星我,我要开启防忽悠。

只是的确很邪恶。据说以后的 还要把默认运算修改为 float...效率啊效率

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值