Forums Register Login

Bitwise Xor of bytes

+Pie Number of slices to send: Send
This is puzzling me,




If I use the bitwise Xor of two bytes why do I get the compile time error




I guess this means that one ^ two returns an int, but I don't understand why.

Thanks

2
+Pie Number of slices to send: Send
It's one of the gotchas of Java. All integer operators result in either an int or a long. So any operator acting on primitives "smaller than" int returns an int.

E.g.
short + short = int
byte * short = int
byte ^ byte = int


I'm not entirely sure why it was designed like that, but there'll probably be someone along to explain it .

The result is, you need to write:
+Pie Number of slices to send: Send
That is rather strange.
Thank you!
+Pie Number of slices to send: Send
And the operators += -= *= /= %= &= ^= |= <<= >>= >>>= have a build it cast.
So byte a = 5; a += 5; is valid.
I yam what I yam and that's all that I yam - the great philosopher Popeye. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 9360 times.
Similar Threads
Trouble Converting from Int Array to Byte Array
Range of bitwise operators
CRC calc problem
conversion
Bitwise
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 06:04:38.