1

While doing bitwise operation on primitive types smaller than int they are promoted to int and after operation they are converted back to their native type.

Why can't we simply do operation on native type?

mari
  • 864
  • 3
  • 12
  • 32
SIGSTP
  • 1,125
  • 2
  • 9
  • 21
  • 1
    Already asked in the site, somewhere to do with limiting the number of opcodes (instructions), search it if you are interested. – SJuan76 Jun 22 '13 at 13:59
  • 3
    the answer is given here: [byte bitwise operations produce int][1] [1]: http://stackoverflow.com/questions/2003003/why-does-the-xor-operator-on-two-bytes-produce-an-int – Tala Jun 22 '13 at 14:02
  • first googled it but couldn't find answer. Thnx for link. – SIGSTP Jun 22 '13 at 14:05

1 Answers1

1

Common CPUs uses 32-bit number representation for that kind of arithmetical operations. That means that registers in the Arithmetic Logic Unit (ALU) of the CPU are most likely 32-bit and that's how CPU does it. It's obvious that implicit conversion happened.

Why Java does not convert it back to that "smaller" type, I have no clue. That's the question for creators of Java.

darijan
  • 9,725
  • 25
  • 38