bitwise-and

What does (number & -number) mean in bit programming? [duplicate]

a 夏天 提交于 2019-11-29 19:48:22
This question already has an answer here: meaning of (number) & (-number) 3 answers For example: int get(int i) { int res = 0; while (i) { res = (res + tree[i]) % MOD; i -= ( (i) & (-i) ); } return res; } A tree update function: void update(int i, int val) { while (i <= m) { tree[i] = (tree[i] + val) % MOD; i += ( (i) & (-i) ); } } Can you please explain what they do in the code by using ( (i) & (-i) ) ? This two functions are a modified implementation of a Binary index tree (Fenwick tree) data structure. Here is two pictures to supplement MikeCAT's answer showing how i variable updates for

bitwise-ANDing with 0xff is important?

亡梦爱人 提交于 2019-11-28 21:31:47
Doesn't bitwise-ANDing with 0xff essentially mean getting the same value back, for that matter, in this code? byte[] packet = reader.readPacket(); short sh; sh = packet[1]; sh &= 0xFF; System.out.print(sh+" "); Weirdly, I get a -1 if that ANDing is not included but a 255 when included Could someone explain the reason? As I see it 0xff is just 1111 1111. Isn't it? Yes, 0xff is just 1111 1111 . But this is attempting to display the unsigned byte value, even though in Java byte s are signed. The value 0xff is -1 for a signed byte , but it's 255 in a short . When a byte value of 0xff is read,

Mod of power 2 on bitwise operators?

心已入冬 提交于 2019-11-28 15:39:04
问题 How does mod of power of 2 work on only lower order bits of a binary number ( 1011000111011010 )? What is this number mod 2 to power 0, 2 to power 4? What does power of 2 have to do with the modulo operator? Does it hold a special property? Can someone give me an example? The instructor says "When you take something mod to power of 2 you just take its lower order bits". I was too afraid to ask what he meant =) 回答1: He meant that taking number mod 2^n is equivalent to stripping off all but the

What does (number & -number) mean in bit programming? [duplicate]

帅比萌擦擦* 提交于 2019-11-28 15:28:38
问题 This question already has an answer here: meaning of (number) & (-number) 3 answers For example: int get(int i) { int res = 0; while (i) { res = (res + tree[i]) % MOD; i -= ( (i) & (-i) ); } return res; } A tree update function: void update(int i, int val) { while (i <= m) { tree[i] = (tree[i] + val) % MOD; i += ( (i) & (-i) ); } } Can you please explain what they do in the code by using ( (i) & (-i) ) ? 回答1: This two functions are a modified implementation of a Binary index tree (Fenwick

What does value & 0xff do in Java?

♀尐吖头ヾ 提交于 2019-11-26 03:07:34
I have the following Java code: byte value = 0xfe; // corresponds to -2 (signed) and 254 (unsigned) int result = value & 0xff; The result is 254 when printed, but I have no idea how this code works. If the & operator is simply bitwise, then why does it not result in a byte and instead an integer? It sets result to the (unsigned) value resulting from putting the 8 bits of value in the lowest 8 bits of result . The reason something like this is necessary is that byte is a signed type in Java. If you just wrote: int result = value; then result would end up with the value ff ff ff fe instead of 00

What does value & 0xff do in Java?

穿精又带淫゛_ 提交于 2019-11-26 01:51:09
问题 I have the following Java code: byte value = 0xfe; // corresponds to -2 (signed) and 254 (unsigned) int result = value & 0xff; The result is 254 when printed, but I have no idea how this code works. If the & operator is simply bitwise, then why does it not result in a byte and instead an integer? 回答1: It sets result to the (unsigned) value resulting from putting the 8 bits of value in the lowest 8 bits of result . The reason something like this is necessary is that byte is a signed type in