Bitwise Operations on short

爷,独闯天下 提交于 2019-12-05 04:11:12
Nayuki

When doing any arithmetic on byte, short, or char, the numbers are promoted to the wider type int. To solve your problem, explicitly cast the result back to short:

bit = (short)(bit | 0x00000001);

Links:

My understanding is that java does not support short literal values. But this did work for me:

short bit = 0;
short one = 1;
short two = 2;
short other = (short)~one;
System.out.println(bit);
bit |= one;
System.out.println(bit);
bit &= other;
bit |= two;
System.out.println(bit);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!