Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I'm studying java now and I encountered this
n &= n - 1;
. I don't know what is the meaning of this. I just saw this on so.

What I have tried:

I think it's a simplified version but I just don't know how to make it compound again.
Posted
Updated 12-Aug-20 21:39pm

All of the <operator>= operations are the same:
Java
a op= b;
expands to
Java
a = a op b;

So
Java
a += 2;
Is adding 2 to a
Java
a *= 2;
multiplies a by 2
So your code is a simplified version of this:
Java
n = n & (n - 1);

What it's doing is performing an binary (or bitwise) AND between n and n - 1
Why you have code doing that is anyone's guess!
 
Share this answer
 
Comments
lelouch_vi 2 13-Aug-20 6:59am    
hello OriginalGriff, I get it now. Crystal clear. I have a hunch that that is the compound version of n &= n - 1. I just want to confirm so I'm not jumping into a mistake. Thanks a lot!
OriginalGriff 13-Aug-20 7:40am    
You're welcome!
OriginalGriff 13-Aug-20 7:43am    
If you are counting one bits, you may find this interesting:

https://www.codeproject.com/Tips/365002/A-Csharp-implementation-of-AI-MEMO-239-Item-169-Co

Have a read!
Extending OG's response. By Bitwise AND operation means here:
C#
uint a = 0b_1111_1000;
uint b = 0b_1001_1101;
uint c = a & b;
Console.WriteLine(Convert.ToString(c, toBase: 2));
// Output:
// 10011000

Quote:
For bool operands, the & operator computes the logical AND of its operands.
The unary & operator is the address-of operator

Refer:
Bitwise and shift operators - C# reference | Microsoft Docs[^]
Bitwise operators in Java - GeeksforGeeks[^]

To explain it further:
Quote:
The output of bitwise AND is 1 if the corresponding bits of two operands is 1. If either bit of an operand is 0, the result of corresponding bit is evaluated to 0.


Let us suppose the bitwise AND operation of two integers 12 and 25.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bit Operation of 12 and 25
  00001100
  00011001
  ========
  00001000  = 8 (In decimal)

Via code:
C#
{
    int a = 12, b = 25, c;
    c = a&b;
    Console.Writeline("Output = "+c); // prints Output = 8
}


So overall, your n &= n-1 translates to: n = n & n-1
In it, value of n=5 is getting reset to:
n = number(bit(5) & bit(4))
=> n = number((0101) & (0100))
       0101
       0100
      ======
       0100

=> n = number ( 0100 ) = 4

Hope that clarifies.
 
Share this answer
 
v4
Comments
lelouch_vi 2 13-Aug-20 7:13am    
wow, thank you for this. Yes, I'm actually solving a problem where I need to count the ones in a binary, say 5 is 0101 the answer should be 2. Funny thing is that I can write a function to get the input number and use the
Integer.bitcount(n)
method to simplify things. I just knew about the method after I finished the problem.
Sandeep Mewara 13-Aug-20 9:03am    
Welcome! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900