Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

    while ((1U << i) < nSize) {         i++;     } 

Any particular reason to use 1U instead of 1?

like image 585
arr Avatar asked Nov 16 '10 08:11

arr


People also ask

What is 5u in C?

5u means b00000101 , and because -1u is b11111111 in binary, you just subtract 4 from it, so -5u is b11111011 , which as an int , it is 251 .

What does 0u mean in C?

Much like 0L defines 0 as a long, 0u defines 0 as an unsigned int ( uint ).

What is the difference between-1L and 1uL in C++?

For example, suppose that int is 16 bits and long is 32 bits. Then -1L < 1U, because 1U, which is an unsigned int, is promoted to a signed long. But -1L > 1UL because -1L is promoted to unsigned long and thus appears to be a large positive number.

What does 1U mean on a number?

what does the "1u" mean? And when should we use it instead of just use "1"? It means the digit, associated with it, is unsigned. The use of unsigned varies upon necessity.

What is the difference between 1U and 2U servers?

"What is a '2U' server? What is the difference?" The 'U' in any server description is short for "RU" which stands for Rack Unit -- this is the standardized designation for the form factor of the server: 1U = 1.75" in height or thickness. 2U is 1.75" x2 = 3.5 inches. All rackmount servers are 19" in width.

What is 1U 2U 3U 4U 5U 6U and 7U?

What is 1U, 2U, 3U, 4U, 5U, 6U, and 7U? The 1U, 2U, 3U, 4U, 5U, 6U and 7U are all different sized rackmount servers, and the U following the number is short for unit. The number indicates the size of the rackmount, 1U being the smallest rackmount and 7U being the biggest rackmount.


1 Answers

On most compliers, both will give a result with the same representation. However, according to the C specification, the result of a bit shift operation on a signed argument gives implementation-defined results, so in theory 1U << i is more portable than 1 << i. In practice all C compilers you'll ever encounter treat signed left shifts the same as unsigned left shifts.

The other reason is that if nSize is unsigned, then comparing it against a signed 1 << i will generate a compiler warning. Changing the 1 to 1U gets rid of the warning message, and you don't have to worry about what happens if i is 31 or 63.

The compiler warning is most likely the reason why 1U appears in the code. I suggest compiling C with most warnings turned on, and eliminating the warning messages by changing your code.

like image 103
Dietrich Epp Avatar answered Sep 20 '22 07:09

Dietrich Epp