首页
社区
课程
招聘
[讨论]python的无符号数
发表于: 2008-7-3 00:35 10235

[讨论]python的无符号数

2008-7-3 00:35
10235
在 idle 里输入

>>> '%x' % -100
'-64'

fuck, '-64' 是什么玩意儿? 解决方法如下:

>>> '%x' % (-100 & 0xff)
'9c'

[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!

收藏
免费 0
支持
分享
最新回复 (6)
雪    币: 220
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
#python
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> '%x' % -100
'-64'
>>> '%x' % ( -100 & 0xff )
'9c'
>>> type(-100)
<type 'int'>
>>> type( -100 & 0xff )
<type 'int'>

都是有符号?不懂
2008-7-3 11:54
0
雪    币: 8209
活跃值: (4513)
能力值: ( LV15,RANK:2473 )
在线值:
发帖
回帖
粉丝
3
不懂python的飘过
2008-7-3 12:12
0
雪    币: 325
活跃值: (97)
能力值: ( LV13,RANK:530 )
在线值:
发帖
回帖
粉丝
4
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...效率啊效率
2008-7-3 23:11
0
雪    币: 66
活跃值: (15)
能力值: ( LV9,RANK:330 )
在线值:
发帖
回帖
粉丝
5
解释语言里面正负号挺让人不放心~

if($op eq "neg") {
        if($rlen == 32)    { $result = 0xFFFFFFFF - int($imm1) + 1; }
        elsif($rlen == 16) { $result = 0xFFFF     - int($imm1) + 1; }
        elsif($rlen == 8)  { $result = 0xFF       - int($imm1) + 1; }
}

带py的东东不多, 多半猜到在搞什么了
2008-7-4 09:53
0
雪    币: 846
活跃值: (221)
能力值: (RANK:570 )
在线值:
发帖
回帖
粉丝
6
支持depython
2008-7-4 12:07
0
雪    币: 6075
活跃值: (2236)
能力值: (RANK:1060 )
在线值:
发帖
回帖
粉丝
7
没有搞什么depython,只是模拟抽奖发现没法把binascii的crc32结果做为unsigned long
2008-7-4 17:24
0
游客
登录 | 注册 方可回帖
返回
//