bitmap - Understanding this bit mask method - Stack Overflow

admin2025-04-21  3

I saw an implementation of a bit mask in the following code, but I don't understand how it works. Can someone explain how the expression in the bit mask works? The author's comments are at the top of the method. The array is made of 3 portions member[0], member[1] and member[2] each with 8 bits for a total of 0-23 bits, i.e. for member[0] bits 0-7, member[1] bits 8-15 and member [2] bits 16-23

                   // use & to find the remainder after dividing by
                   // 8. remainder 0 puts a 1 in the left-most bit
                   // and 7 puts a 1 in the right-most bit

                   byte bitMask(int i)
                   {
                    return (byte) (1 << (7 - (i & 7)));
                   }

I don't quite understand the authors explanation in his comments above and would like if someone would deconstruct the expression in some meaningful form

转载请注明原文地址:http://www.anycun.com/QandA/1745223729a90431.html