[英]Is subtracting larger unsigned value from smaller in C++ undefined behaviour?
[英]Is "(unsigned)value" cast undefined behaviour?
我正在編寫一些代碼來測試位域中的位,我寫了這個:
return (unsigned)(m_category | category) > 0
我雖然這不會編譯,因為我沒有給出整數類型,但令我驚訝的是它做到了。 所以我想知道,這是做什么的? 它是未定義的嗎?
unsigned
與unsigned int
相同。
強制轉換會將有符號整數轉換為無符號整數,如下所示:
https://en.cppreference.com/w/cpp/language/implicit_conversion#Integral_conversions
If the destination type is unsigned, the resulting value is the smallest unsigned value equal to the source value modulo 2n where n is the number of bits used to represent the destination type. That is, depending on whether the destination type is wider or narrower, signed integers are sign-extended[footnote 1] or truncated and unsigned integers are zero-extended or truncated respectively.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.