如何在Java中将最后一个字节的最后一个字节更改为零?

发布于 2025-01-17 16:40:16 字数 514 浏览 3 评论 0原文

我有一个字节:

“ 10000110”,是十进制中的97

我不知道正确的订购,有些地方写成“ 01100001”,关键是我说的是十进制数字97,该数字97存储在我们的应用中的一个字节中。

在这个数据的位置,我们不需要超过100的数字,我们想在相应的字节中存储上一个(8th)位在相应的字节中存储一个额外的布尔数据

  1. 。想在之后的第8位中写一个0,我已经为布尔信息读了这一点。

因此,这样,我将获得第8位的布尔数据,然后我可以将第8位至零写入零,因此我可以阅读我的字节以获取小数号。 (因为我们仅使用0-100的值,所以最后位应始终为零。)

我不知道它是否足够清楚,请告诉我您是否有任何疑问。


因此,简而言之,我的问题是:

我如何在Java中写下0到最后一个字节的最后一点?

I have a byte for example:

"10000110" which is 97 in decimal.

I don't know the correct ordering, some places it is written "01100001", the point is that I'm speaking about the decimal number 97 which is stored in a single byte in our app.

We don't need bigger numbers than 100 in our app in this place of data and we would like to store an additional boolean data in the last (8th) bit in the corresponding byte, so..

  1. I would like to write a 0 in the 8th bit after I've read that bit for the boolean information.

So this way I will have my boolean data from the 8th bit, and after that I can write the 8th bit to zero, so I can read my byte to get the decimal number. (because we only use values from 0-100 the last bit should always be zero.)

I don't know if it is clear enough, please let me know if you have any questions.


So in short, my question is:

How could I write a 0 to the last bit of a byte in Java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻泪 2025-01-24 16:40:16

97小数为61(十六进制)或1100001(二进制),

如果您想使用高订单位(10000000二进制或80 HEX)存储其他信息,则可以如下这样做:

byte b = 97;
// Set the high-order bit to 1
byte c = b | 0x80;

// Determine if the high-order bit is set or not
boolean bitIsSet = (c & 0x80) != 0;

// clear the high-order bit (remove the high-order bit to get the value)
byte d = c & 0x7F;

我希望这很清楚并回答您的问题。

97 decimal is 61 (hexadecimal) or 1100001 (binary)

If you want to use the high-order bit (10000000 binary or 80 hex) to store other information, you can do this as follows:

byte b = 97;
// Set the high-order bit to 1
byte c = b | 0x80;

// Determine if the high-order bit is set or not
boolean bitIsSet = (c & 0x80) != 0;

// clear the high-order bit (remove the high-order bit to get the value)
byte d = c & 0x7F;

I hope this is clear and answers your question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文