Is there any difference between 1U and 1 in C?

后端 未结 4 1027
别跟我提以往
别跟我提以往 2020-12-28 16:51
    while ((1U << i) < nSize) {
        i++;
    }

Any particular reason to use 1U instead of 1?

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-28 17:01

    1U is unsigned.

    The reason why they used an unsigned value in that is expression is (I guess) because nSize is unsigned too, and compilers (when invoked with certain parameters) give warnings when comparing a signed and an unsigned values.

    Another reason (less likely, in my opinion, but we cannot know without knowing wath value nSize is supposed to assume) is that unsigned values can be twice as big as signed ones, so nSize could be up to ~4*10^9 instead of ~2*10^9.

提交回复
热议问题