正确的方法采取INT

编程入门 行业动态 更新时间:2024-10-09 14:22:14
本文介绍了正确的方法采取INT_MIN绝对值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我要在无符号执行一些算术和需要采取消极为int的绝对值,像

do_some_arithmetic_in_unsigned_mode(INT some_signed_value){   无符号整型幅度;   INT阴性;   如果(some_signed_value℃,){       幅度= 0 - some_signed_value;       负= 1;    }其他{       幅度= some_signed_value;       负= 0;    }   ...略...}

但INT_MIN可能是有问题的,0 - INT_MIN是UB如果符号算术执行。什么是标准/强健/安全/高效的C ++做的这种方式?

编辑:

如果我们知道我们是在2补,也许隐式转换和显位OPS是标准?如果可能的话,我想避免这种假设。

do_some_arithmetic_in_unsigned_mode(INT some_signed_value){   无符号整型幅度= some_signed_value;   诠释负= some_signed_value℃的;   如果(负){       幅度=(〜幅度)+ 1;    }   ...略...}

解决方案

转换从符号到无符号是明确的:你得到相应的再presentative模2 的 N 的。因此,下面会给你正确的绝对值 N :

INT N = / * ... * /;unsigned int类型abs_n = N< 0? UINT_MAX - ((无符号整数)(N))+ 1U                           (无符号整数)(N);

的更新:的作为@ aka.nice表明,我们实际上可以替换 UINT_MAX + 1U 按 0 U :

unsigned int类型abs_n = N< 0: - ((无符号整数)(N)):(unsigned int类型)(N);

I want to perform some arithmetic in unsigned, and need to take absolute value of negative int, something like

do_some_arithmetic_in_unsigned_mode(int some_signed_value) { unsigned int magnitude; int negative; if(some_signed_value<0) { magnitude = 0 - some_signed_value; negative = 1; } else { magnitude = some_signed_value; negative = 0; } ...snip... }

But INT_MIN might be problematic, 0 - INT_MIN is UB if performed in signed arithmetic. What is a standard/robust/safe/efficient way to do this in C?

EDIT:

If we know we are in 2-complement, maybe implicit cast and explicit bit ops would be standard? if possible, I'd like to avoid this assumption.

do_some_arithmetic_in_unsigned_mode(int some_signed_value) { unsigned int magnitude=some_signed_value; int negative=some_signed_value<0; if (negative) { magnitude = (~magnitude) + 1; } ...snip... }

解决方案

Conversion from signed to unsigned is well-defined: You get the corresponding representative modulo 2N. Therefore, the following will give you the correct absolute value of n:

int n = /* ... */; unsigned int abs_n = n < 0 ? UINT_MAX - ((unsigned int)(n)) + 1U : (unsigned int)(n);

Update: As @aka.nice suggests, we can actually replace UINT_MAX + 1U by 0U:

unsigned int abs_n = n < 0 : -((unsigned int)(n)) : (unsigned int)(n);

更多推荐

正确的方法采取INT

本文发布于:2023-11-29 06:17:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1645580.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   方法   INT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!