13

I was reading through the hadoop code and found this line in a partitioner.

(key.hashCode() & Integer.MAX_VALUE) % numReduceTasks

Why are they using the bitwise AND?

jshen
  • 11,507
  • 7
  • 37
  • 59

1 Answers1

18

To remove the sign bit. in the case that the hashCode is a negative number. its like Math.abs(key.hashCode())

The Scrum Meister
  • 29,681
  • 8
  • 66
  • 64
  • @chris-thompson i wonder what `Math.abs` does? it prob. does the same bitwise and. – The Scrum Meister Feb 05 '11 at 01:15
  • `Math.abs` source code is a one-liner: `return (a < 0) ? -a : a;` where `a` is the `int` passed in. – Jonathon Faust Feb 05 '11 at 01:17
  • 3
    They aren't actually doing `Math.abs` because of two's complement -- they just want any positive number (while respecting the probability distribution of the hashing function more or less). – ide Feb 05 '11 at 01:17
  • 7
    `Math.abs()` has a strange habit to return a negative value on `Integer.MIN_VALUE` (namely this same value), since it is immune to the `-` operator. Thus the variant in the question is actually more secure, aside from being probably a bit faster, since no conditional is to be evaluated. – Paŭlo Ebermann Feb 05 '11 at 01:23
  • 1
    This also avoids a method call, although Math.abs() can potentially be inlined by the Jit. – PhiLho May 26 '11 at 11:47
  • no, it's not like `abs()` because it just removes the sign bit, not negate or flip it. For example `-2 & Integer.MAX_VALUE` will return `2,147,483,646`, not 2 – phuclv Sep 14 '14 at 17:32