-6

basically trying to find hamming string https://en.wikipedia.org/wiki/Hamming_distance. I would like to know what does this expression mean and do val &= val - 1;

I know val+=10-->val=val+10

Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
Nick
  • 484
  • 5
  • 18
  • 1
    what language is this? – Scary Wombat Jan 25 '18 at 07:28
  • 1
    This isnt Java, looks like C to me – ug_ Jan 25 '18 at 07:29
  • This doesn't look like Java.. – Taslim Oseni Jan 25 '18 at 07:30
  • This is a AND logical operation. https://en.wikipedia.org/wiki/Logical_conjunction – Oscar Jan 25 '18 at 07:31
  • @machine_1 those asterisks where only here to emphasize the part of code the user didn't understand – Rafalon Jan 25 '18 at 07:36
  • 1
    Lets close this as its been asked before, and all of it, including posted answers are approaching trash quality... – Lundin Jan 25 '18 at 07:39
  • 1
    Why people are down voting the question? It is valid, please do not mark down voted. – Nick Jan 25 '18 at 07:40
  • 1
    @Nick Likely because it can be answered with a minimum of research/Google "what does &= mean in c" or by reading a beginner-level C book. Also you likely got lots of down votes from tagging this Java; as we can see by the answers posted by the Java programmers, they don't have a clue about this. – Lundin Jan 25 '18 at 07:42
  • If you edit your question for adding more useful information (a good thing), please be careful not to loose helpful edits by other users. In this case somebody fixed the broken syntax for you and you lost that. On top of that, your edit made the indentation worse. Also, removing largely helpful code is not advisable. The users here are programmers. Quite a lot of them here love code so much that they insist one seeing some in a question and otherwise will not like it. Ideally try to make a [mcve]. – Yunnosch Jan 25 '18 at 07:42
  • Did you really ask a question to find out what the `&=` operator does? Or did you wonder what the (known) effect of that operator was to achieve in the algorirhm? The latter would be a much more interesting question and (I think) not eligible for being closed as a duplicate. If you have special reasons for "It is valid", then please explain your reasons. Then remember what you read about good questions when you took the [tour] and try to apply those things while [edit]ing your question. – Yunnosch Jan 25 '18 at 07:46
  • You're right @Lundin . Guilty as charged :D . – Taslim Oseni Jan 25 '18 at 15:43

5 Answers5

5

val is initialized to

val = x ^ y;

i.e. val is the bit-wise XOR of x and y, which means it contains 1 in all the bits where x and y differ and 0 in all the bits where x and y are the same.

Now, each iteration of the loop performs

val &= val - 1;

which is equivalent to

val = val & (val - 1);

Each such bit-wise AND operation turns a single 1 bit of val to 0.

For example, suppose the binary representation of val is:

1010

The binary representation of val - 1 is:

1001

and after performing bit-wise AND, we get:

1000

Therefore, by the end of the loop, dist contains the number of 1 bits in the original value of val, which is the number of bits by which x and y differ.

Eran
  • 387,369
  • 54
  • 702
  • 768
3
val &= val - 1

Does the following :

Assigns the result of the bitwise AND operation between val and val - 1 to val

Rafalon
  • 4,450
  • 2
  • 16
  • 30
2

I think, the exact intention is asked for.

val &= val - 1;

This removes the right-most, least-significant bit:

val:           xxxx1000..000
val - 1:       xxxx0111..111
val & (val-1): xxxx0000..000

It typically is used to count (destructively) the number of bits.

The same trick: to get the least significant bit itself, use Integer.lowestOneBit(val):

int lowestOneBit = val & -val;

To understand the latter: -n == ~n + 1. That is: a negative number in java is twos-complement: the one-complement (~n) of every bit plus one.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

It's equivalent to following one, where & is a bitwise AND.

val = val & (val  - 1)
lukeg
  • 4,189
  • 3
  • 19
  • 40
0

&= is a bitwise AND assignment operator and it works basically how compound operators (+=, -=, etc) work in most programming languages.

val &= val - 1; is same as val = val & (val - 1);

A simple '&' comparison is done between val and val - 1 and the result becomes the value of val.

Check this link for knowledge on more operators in C as well as how to use them. Merry coding!

Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69