在Java long值中设置特定位

时间:2021-06-30 11:49:12

I have one long value and Set Particular bit by converting hexadecimal value.

通过转换十六进制值,我有一个long值和Set Particular位。

long l = 4;

long output; //output is 84 if i want set 7th bit (1000 0100) 

same way is long is 7 then output is 87 so how to set particular bit inside long value.

同样的方法是7然后输出是87所以如何设置长值内的特定位。

Requirement:

I have to send one byte to server by proper formatting.

我必须通过适当的格式化向服务器发送一个字节。

Client gives following thing. 1. Whether 7th bit set or not set. 2. One integer value (like 4,5,6,7 etc.) Now I have generate string or decimal (2H) that format as client parameter.

客户提供以下内容。 1.是否设置第7位。 2.一个整数值(如4,5,6,7等)现在我生成了格式为客户端参数的字符串或十进制(2H)。

1 个解决方案

#1


You need to do a bitwise or with the value of the bit.

您需要按位或使用该位的值。

The value of the bit you can find by shifting 1L the correct number of bits to the left. (Don't forget the L, without that you're shifting the int 1.)

通过将1L正确的位数向左移位,可以找到该位的值。 (不要忘记L,没有你,你正在转移int 1.)

Bitwise or can be done with the | operator in Java.

按位或可以用|来完成Java中的运算符。

So the code becomes:

所以代码变成:

long output = l | (1L << 7);

#1


You need to do a bitwise or with the value of the bit.

您需要按位或使用该位的值。

The value of the bit you can find by shifting 1L the correct number of bits to the left. (Don't forget the L, without that you're shifting the int 1.)

通过将1L正确的位数向左移位,可以找到该位的值。 (不要忘记L,没有你,你正在转移int 1.)

Bitwise or can be done with the | operator in Java.

按位或可以用|来完成Java中的运算符。

So the code becomes:

所以代码变成:

long output = l | (1L << 7);