Java getChars method in Integer class, why is it using bitwise operations instead of arithmetic?

前端 未结 2 1458
长情又很酷
长情又很酷 2021-01-21 05:19

So I was examining the Integer\'s class source code (JDK 8) to understand how an int get converted to a String. It seems to b

2条回答
  •  滥情空心
    2021-01-21 05:46

    There are a couple reasons that this is done. Being a long-time embedded developer, using tiny microcontrollers that sometimes didn't even have a multiplication and division instruction, I can tell you that this is significantly faster. The key here is that the multiplier is a constant. If you were multiplying two variables, you'd either need to use the slower multiply and divide operators or, if they didn't exist, perform multiplication using a loop with the add operator.

提交回复
热议问题