无符号整数? [英] unsigned integer?

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

问题描述

这是一个天真的问题:


"%u" %-3


我希望它打印3.但它仍然打印-3。


另外,如果我有一个int,我可以将它转换为C中的unsigned int:

int i = -3;

int ui =(unsigned int)i;


有没有办法在Python中执行此操作?

This is a naive question:

"%u" % -3

I expect it to print 3. But it still print -3.

Also, if I have an int, I can convert it to unsigned int in C:
int i = -3;
int ui = (unsigned int)i;

Is there a way to do this in Python?

推荐答案

3月10日上午11:32,杰克 < nos ... @ invalid.comwrote:
On Mar 10, 11:32 am, "Jack" <nos...@invalid.comwrote:

这是一个天真的问题:


"%u" %-3


我希望它打印3.但它仍然打印-3。


另外,如果我有一个int,我可以将它转换为C中的unsigned int:

int i = -3;

int ui =(unsigned int)i;


有没有办法在Python中执行此操作?
This is a naive question:

"%u" % -3

I expect it to print 3. But it still print -3.

Also, if I have an int, I can convert it to unsigned int in C:
int i = -3;
int ui = (unsigned int)i;

Is there a way to do this in Python?



def unsigned(n):

return n& 0xFFFFFFFF

def unsigned(n):
return n & 0xFFFFFFFF


" Jack" < no **** @ invalid.comwrote:
"Jack" <no****@invalid.comwrote:

这是一个天真的问题:


"%u" ; %-3


我希望它打印3.但它仍然打印-3。
This is a naive question:

"%u" % -3

I expect it to print 3. But it still print -3.



在内部,它使用C运行时来格式化数字,但是如果数字

你要求它打印unsigned是负数它使用% d而不是%u。我有点b $ b不知道是否实际上可能为%d提供不同的输出而不是

%u。

Internally it uses the C runtime to format the number, but if the number
you ask it to print unsigned is negative it uses %d instead of %u. I have
no idea if it is actually possibly to get a different output for %d versus
%u.


>

另外,如果我有一个int,我可以将它转换为C中的unsigned int:

int i = -3;

int ui =(unsigned int)i;


有没有办法在Python中执行此操作?
>
Also, if I have an int, I can convert it to unsigned int in C:
int i = -3;
int ui = (unsigned int)i;

Is there a way to do this in Python?



取决于你想要它如何转换:


i = -3

ui = abs(i)

print ui

ui =(i& 0xffff)#对于16位整数

print ui

ui =(i& 0xffffffff)#对于32位整数

打印ui

ui =(i& 0xffffffffffffffff)#64位整数

print ui

ui =(i& 0xffffffffffffffffffffffffffffffff)#128位整数

打印ui

给出以下输出:


3

65533

4294967293

18446744073709551613

340282366920938463463374607431768211453

有一种将Python整数转换为无符号值的独特方式

这就是%u格式字符串无法做任何事情的原因除了打印

值。就个人而言,我已经预料到Python要么打印

绝对值或者抛出异常,但我想把它作为%d的别名

有点制作感觉也是如此。

Depeneding on how exactly you want it converted:

i = -3
ui = abs(i)
print ui
ui = (i & 0xffff) # for 16 bit integers
print ui
ui = (i & 0xffffffff) # for 32 bit integers
print ui
ui = (i & 0xffffffffffffffff) # for 64 bit integers
print ui
ui = (i & 0xffffffffffffffffffffffffffffffff) # for 128 bit integers
print ui

which gives the following output:

3
65533
4294967293
18446744073709551613
340282366920938463463374607431768211453

There isn''t a unique way to convert a Python integer to an unsigned value
which is why the %u format string cannot do anything other than print the
value. Personally I''d have expected the Python one to either print the
absolute value or throw an exception, but I guess making it an alias for %d
kind of makes sense as well.


Dan Bishop写道:
Dan Bishop wrote:

3月10日上午11点32分,杰克" < nos ... @ invalid.comwrote:
On Mar 10, 11:32 am, "Jack" <nos...@invalid.comwrote:

>这是一个天真的问题:

"%u" %-3

我希望它打印3.但它仍然打印-3。

另外,如果我有一个int,我可以将它转换为C中的unsigned int :
int i = -3;
int ui =(unsigned int)i;

有没有办法在Python中执行此操作?
>This is a naive question:

"%u" % -3

I expect it to print 3. But it still print -3.

Also, if I have an int, I can convert it to unsigned int in C:
int i = -3;
int ui = (unsigned int)i;

Is there a way to do this in Python?



def unsigned(n):

返回n& 0xFFFFFFFF


def unsigned(n):
return n & 0xFFFFFFFF



或abs(-1)?


or abs(-1) ?


这篇关于无符号整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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