在 C++ 中,避免整数溢出将无符号 int 转换为 int 的有效方法是什么?

发布于 2024-10-20 00:18:59 字数 606 浏览 6 评论 0原文

以下是在 C++ 中将 unsigned int 转换为 int 的有效且无问题的方法:

#include <limits.h>
void safeConvert(unsigned int passed) 
{
    int variable = static_cast<int>(passed % (INT_MAX+1)); 
    ...
}

还是有更好的方法?

更新

正如 James McNellis 所指出的,分配一个 unsigned int > 并不是未定义的。 INT_MAX 为整数 - 相反,这是实现定义的。因此,这里的上下文现在特别适合我的偏好,以确保当无符号 int 超过 INT_MAX 时该整数重置为零。

原始上下文

我有许多用作计数器的无符号 int,但希望在特定情况下将它们作为整数传递。

在正常操作下,这些计数将保持在 INT_MAX 范围内。但是,为了避免在发生异常(但有效)情况时遇到 undefined 实现特定行为,我希望在这里进行一些有效的转换。

Is the following an efficient and problem free way to convert an unsigned int to an int in C++:

#include <limits.h>
void safeConvert(unsigned int passed) 
{
    int variable = static_cast<int>(passed % (INT_MAX+1)); 
    ...
}

Or is there a better way?

UPDATE

As pointed out by James McNellis it is not undefined to assign an unsigned int > INT_MAX to an integer - rather this is implementation defined. As such the context here is now specifically on my preference is to ensure this integer resets to zero when the unsigned int exceeds INT_MAX.

Original Context

I have a number of unsigned int's used as counters, but want to pass them around as integers in a specific case.

Under normal operation these counts will remain within the bounds of INT_MAX. However to avoid running into undefined implementation specific behaviour should the abnormal (but valid) case occur I want some efficient conversion here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁梦 2024-10-27 00:18:59

这也应该有效:

int variable = passed & INT_MAX;

This should also work:

int variable = passed & INT_MAX;
临走之时 2024-10-27 00:18:59

在正常操作下,这些计数将保持在 INT_MAX 范围内。但是,为了避免在发生异常(但有效)情况时遇到未定义的行为,我希望在这里进行一些有效的转换。

高效转化为什么?如果 int 和 unsigned int 的所有共享值都对应,并且您希望其他无符号值(例如 INT_MAX + 1)各自具有不同的值,那么您只能将它们映射到负整数值。这是默认完成的,并且可以使用 static_cast(my_unsigned) 显式请求。否则,您可以将它们全部映射为 0、-1、INT_MIN,或者丢弃高位...最简单的方法很简单:if (my_unsigned > INT_MAX) my_unsigned = XXX,或...my_unsigned &= INT_MAX 清除高位。但是如果int溢出,被调用的函数还能正常工作吗?也许更好的解决方案是首先使用 64 位整数?

Under normal operation these counts will remain within the bounds of INT_MAX. However to avoid running into undefined behaviour should the abnormal (but valid) case occur I want some efficient conversion here.

Efficient conversion to what? If all the shared values for int and unsigned int correspond, and you want other unsigned values such as INT_MAX + 1 to each have distinct values, then you can only map them onto the negative integer values. This is done by default, and can be explicitly requested with static_cast<int>(my_unsigned). Otherwise, you could map them all to 0, or -1, or INT_MIN, or throw away the high bit... easiest way is simply: if (my_unsigned > INT_MAX) my_unsigned = XXX, or ...my_unsigned &= INT_MAX to clear the high bit. But will the called functions work properly if the int overflows? Perhaps a better solution would be to use 64-bit ints to begin with?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文