Is there any difference between 1U and 1 in c? -
while ((1u << i) < nsize) { i++; }
any particular reason use 1u
instead of 1
?
on compliers, both give result same representation. however, according c specification, result of bit shift operation on signed argument gives implementation-defined results, in theory 1u << i
more portable 1 << i
. in practice c compilers you'll ever encounter treat signed left shifts same unsigned left shifts.
the other reason if nsize
unsigned, comparing against signed 1 << i
generate compiler warning. changing 1
1u
gets rid of warning message, , don't have worry happens if i
31 or 63.
the compiler warning reason why 1u
appears in code. suggest compiling c warnings turned on, , eliminating warning messages changing code.
Comments
Post a Comment