“unsigned int” prints as a negative number?

前端 未结 3 1753
无人及你
无人及你 2021-01-06 00:48

I am taking an integer, in this case 192, and left shifting it 24 spaces. The leading 1 is causing it to become negative, it seems.

unsigned int i = 192;
uns         


        
相关标签:
3条回答
  • 2021-01-06 01:25

    %d means "signed integer"; use %u for "unsigned integer".

    0 讨论(0)
  • 2021-01-06 01:33

    Because you reinterpret it in NSLog as a signed integer. You should use %u to see an unsigned value.

    There is no way for a function with variable number of arguments to know with certainty the type of the value that you pass. That is why NSLog relies on the format string to learn how many parameters you passed, and what their types are. If you pass a type that does not match the corresponding format specifier, NSLog will trust the specifier and interpret your data according to it. Modern compilers may even warn you about it.

    0 讨论(0)
  • 2021-01-06 01:37

    You wan to do NSLog(@"newnumber is %u",newnumber);

    %d converts it back to a signed int.

    0 讨论(0)
提交回复
热议问题