English 中文(简体)
如何从一旁获得某一位置的借方的价值?
原标题:How to get the value of a bit at a certain position from a byte?
  • 时间:2012-02-20 00:39:32
  •  标签:
  • java
  • byte
  • bit

如果我有 by,那么这种方法将如何在某个职位上检索一个轨道?

我知道这一点,我认为这行不通。

public byte getBit(int position) {
    return (byte) (ID >> (position - 1));
}

www.un.org/Depts/DGACM/index_french.htm

最佳回答
public byte getBit(int position)
{
   return (ID >> position) & 1;
}

按职位分列的正确身份证明文件将使得在最远的地方对编号正确。 将“双向”和“<条码”与“;

position = 2
ID = 5 = 0000 0101 (in binary)
ID >> position = 0000 0001

0000 0001 & 0000 0001( 1 in binary ) = 1, because the furthest right bit is set.
问题回答

你们想作一面掩盖,做两点。 这最终将非常接近你——利用轮班确定适当的比照,使用<代码>&进行双向交易。

So

 return ((byte)ID) & (0x01 << pos) ;

where pos has to range between 0 and 7. If you have the least significant bit as "bit 1" then you need your -1 but I d recommend against it -- that kind of change of position is always a source of errors for me.

a. 进入绝缘体

 return ((num >> (n-1)) & 1);

In Java the following works fine:

if (value << ~x < 0) {
   // xth bit set
} else {
   // xth bit not set
}

int或long。 (也不一定相同)。

www.un.org/Depts/DGACM/index_spanish.htm 非Java方案设计员的告诫:在 Java的前面的表述作品,因为用该语言,轨道转换操作者只适用于右侧操作体最低的5条(或6条,long)。 默示将这一表述转化为<代码>、数值和带;< (~x & 31)(或、数值和带;< (~x & 63)

<>strong>Javascript:它也在javascript(如java,只有最低5倍的转移计数适用)。 在javascript中, 编号为32-bit。

特别是在C,消极的转变是援引未经界定的行为,因此这一检验必然取得一定的工作(但取决于您汇编者/加工者的具体组合)。

采取下列步骤:

  1. Create bitMask
  2. Do & (AND) bitwise operation

In the number 5, the positions are from right to left (as given by index) Number (5) = |0|1|0|1| Positions = |3|2|1|0|

现在,你想在第3位(指数=2)获得比值。

int bitMask = 1 << position
int newNumber = bitMask & number

i.e:

int number = 5
int position = 2
int bitMask = 1 << position
// bitMask value becomes here:
// 1 << 2 (0001 << 2) becomes 0100
int newNumber = bitMask & number
// do AND (& bitwise) operation with bitMask and number
0100 & 0101 becomes 0100
newNumber = 4 (0100)

你们可以恢复真实或虚假的:

return ((bitMask & number) != 0)
// means if the new number is non-zero, it means the bit as position is 1 otherwise false.




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签