Блог пользователя IvanSimic

Автор IvanSimic, история, 3 месяца назад, По-английски

I noticed that a lot of top programmers use unsigned types when you don't need negative numbers, but is it good practice to do so or should you avoid it?

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
3 месяца назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

In short: No.

Why? because it doesn't improves speed or memory at all and also does overflows (underflow) if it goes under 0 and in some problems you need to work with values under 0, (for example: 2033A - Sakurako and Kosuke) and if you use unsigned you are probably going to receive errors, and if you want to use unsigned you have to be careful and it's just some additional headache so I really recommend not using it.

»
3 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Only use them for bit manipulation because the operators will work as you would expect. Look up how 2s complement representation works with bit shifts if you want to learn more.