Java 处理位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4032327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Working with bits
提问by James
Let me start by saying I have never really worked with bits before in programming. I have an object that can be in 3 states and I want to represent those states using a 3 bit array.
For example:
I have a race car and it can go forward,left, and right at a stand still the bits would be 000
If the car was moving forward the bits would be 010 if forward and left it would be 110 etc...
How would I set the bits and how could I read them back to get the values?
首先让我说我以前在编程中从未真正使用过位。我有一个可以处于 3 个状态的对象,我想使用 3 位数组表示这些状态。
例如:
我有一辆赛车,它可以在静止状态下向前、向左和向右行驶,位将为 000
如果汽车向前行驶,则位将为 010,如果向前和向左则为 110 等...
我将如何设置这些位以及如何读取它们以获取值?
采纳答案by KarlP
If size and speed is important, use bits in a byte. (Read the links posted in the other answer as there are non-obvious complications when using and casting signed datatypes.)
如果大小和速度很重要,请在字节中使用位。(阅读其他答案中发布的链接,因为在使用和转换签名数据类型时存在不明显的并发症。)
This encodes for the speeds: stand, left, left_forward, forward, right_forward, and right.
这对速度进行编码:stand、left、left_forward、forward、right_forward 和 right。
public class Moo {
final static byte FORWARD = 0x1; // 00000001
final static byte LEFT =0x2; // 00000010
final static byte RIGHT =0x4; // 00000100
/**
* @param args
*/
public static void main(String[] args) {
byte direction1 = FORWARD|LEFT; // 00000011
byte direction2 = FORWARD|RIGHT; // 00000101
byte direction3 = FORWARD|RIGHT|LEFT; // 00000111
byte direction4 = 0;
// someting happens:
direction4 |= FORWARD;
// someting happens again.
direction4 |= LEFT;
System.out.printf("%x: %s\n", direction1, dirString(direction1));
System.out.printf("%x: %s\n", direction2, dirString(direction2));
System.out.printf("%x: %s\n", direction3, dirString(direction3));
System.out.printf("%x: %s\n", direction4, dirString(direction4));
}
public static String dirString( byte direction) {
StringBuilder b = new StringBuilder("Going ");
if( (direction & FORWARD) > 0){
b.append("forward ");
}
if( (direction & RIGHT) > 0){
b.append("turning right ");
}
if( (direction & LEFT) > 0){
b.append("turning left ");
}
if( (direction &( LEFT|RIGHT)) == (LEFT|RIGHT)){
b.append(" (conflicting)");
}
return b.toString();
}
}
Output:
输出:
3: Going forward turning left
5: Going forward turning right
7: Going forward turning right turning left (conflicting)
3: Going forward turning left
Note also that Left and Right is mutually exclusive, so its possible to create an illegal combination. (7 = 111 )
另请注意,Left 和 Right 是互斥的,因此可能会创建非法组合。(7 = 111)
If you actually meant that a thing can only move LEFT, FORWARD or RIGHT, then you don't need flags, just enums.
如果你的意思是一个东西只能向左、向前或向右移动,那么你不需要标志,只需要枚举。
This enum is possible to transport in only two bits.
该枚举可以仅以两位进行传输。
enum Direction{
NONE, FORWARD, RIGHT, LEFT;
}
Direction dir = Direction.FORWARD;
byte enc = (byte) dir.ordinal();
The final two bits in enc
will become:
最后两位enc
将变为:
00 : none
01 : forward;
10 : right
11 : left
回答by mdrg
The least you'll need to store these three bits is one byte
.
您至少需要存储这三个位是 1 byte
。
Read this tutorialon bitwise operators to get started.
阅读有关按位运算符的本教程以开始使用。
Edit: this pageon bit masks may also be very helpful.
回答by Fred Foo
You say three states, but you've actually got six: forward, forward-left, forward-right, left, right, stand-still. Unless your race car doesn't move sideways ofcourse, then you've got four.
你说三种状态,但实际上有六个:向前、向左、向前、向右、向左、向右、静止。除非你的赛车当然不会侧向移动,否则你就有四个。
You should really use an enumfor this:
您真的应该为此使用枚举:
enum State { FORWARD, FORWARD_LEFT, FORWARD_RIGHT, STAND_STILL }
Since left, right and forward are mutually exclusive, this isn't a very good fit for a bit-fiddling program. You'll get into all kinds of consistency problems.
由于左、右和前进是相互排斥的,因此这不太适合摆弄位的程序。你会遇到各种一致性问题。
回答by Aaron
回答by Emil
I would suggest using BitSetalong with enum's
我建议将BitSet与枚举一起使用
enum State { LEFT, RIGHT, FORWARD,STAND_STILL}
BitSet stat=new BitSet(4);
void setLeft() // and so on for each state
{
stat.set(State.LEFT);
}
boolean isLeft()
{
stat.get(State.LEFT);
}
void reset() //reset function to reset the state
{
stat.clear();
}