What does this boolean “(number & 1) == 0” mean?

前端 未结 9 928
眼角桃花
眼角桃花 2020-12-08 01:27

On CodeReview I posted a working piece of code and asked for tips to improve it. One I got was to use a boolean method to check if an ArrayList had an even number of indices

9条回答
  •  甜味超标
    2020-12-08 02:24

    Single & means bit-wise and operator not comparison

    So this code checks if the first bit (least significant/most right) is set or not, which indicates if the number is odd or not; because all odd numbers will end with 1 in the least significant bit e.g. xxxxxxx1

提交回复
热议问题