如何使用Python检查数字是否为32位整数? [英] How do I check if a number is a 32-bit integer using Python?

查看:74
本文介绍了如何使用Python检查数字是否为32位整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,我正在查看一个字符串,我想知道它是否代表32位整数.

In my program I'm looking at a string and I want to know if it represents a 32-bit integer.

当前,我首先使用isdigit()检查它是否是一个数字,然后检查它是否超过2 ^ 32的值(假设我不在乎无符号值).

Currently I first check if it is a digit at all using isdigit(), then I check if it exceeds the value of 2^32 (assuming I don't care about unsigned values).

检查我的输入字符串是否包含有效的32位整数的最佳方法是什么?

What's the best way to check that my input string contains a valid 32-bit integer?

推荐答案

假定最大的32位整数为 0xffffffff

Assuming the largest 32-bit integer is 0xffffffff,

然后,我们需要检查我们的数字是否大于此值:

Then, we need to check if our number is larger than this value:

abs(n) <= 0xffffffff

在数字周围加上abs()也会处理负面情况.

Wrapping an abs() around the number will take care of negative cases, too.

这篇关于如何使用Python检查数字是否为32位整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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