[英]Implementation based on test Java (bytewise) JUnit test
I'm trying to implement a class in order to pass the following test (Using bytewise operators & and |我正在尝试实现 class 以通过以下测试(使用字节运算符 & 和 |
public void hasFlagTest1() {
byte resource = ResourceUtil.getFlag(FLAG_PUBLIC_SECURITY, FLAG_PRIVATE_SECURITY, FLAG_BASIC_LIFE_SUPPORT);
Assert.assertTrue(ResourceUtil.hasPublicSecurity(resource));
Assert.assertTrue(ResourceUtil.hasPrivateSecurity(resource));
Assert.assertTrue(ResourceUtil.hasBasicLifeSupport(resource));
Assert.assertFalse(ResourceUtil.hasVolunteers(resource));
Assert.assertFalse(ResourceUtil.hasAllOpts(resource));
}
The constant values that are passed by parameters are参数传递的常量值是
public static final byte FLAG_PRIVATE_SECURITY = 1;
public static final byte FLAG_PUBLIC_SECURITY = 2;
public static final byte FLAG_BASIC_LIFE_SUPPORT = 4;
public static final byte FLAG_VOLUNTEERS = 8;
public static final byte FLAG_ALL_OPTS = 15;
I have already created a class but not sure how to implement all methods in order to make this test pass, here's the class:我已经创建了一个class但不确定如何实现所有方法才能通过此测试,这里是 class:
public class ResourceUtil {公共 class ResourceUtil {
public static byte getFlag(byte arg, byte arg2, byte arg3){
return result; //just a value, not implemented
};
public static boolean hasPublicSecurity(byte resource) {
return true; //just a value, not implemented
}
public static boolean hasPrivateSecurity(byte resource) {
return true; //just a value, not implemented
}
public static boolean hasBasicLifeSupport(byte resource) {
return true; //just a value, not implemented
}
public static boolean hasVolunteers(byte resource) {
return true; //just a value, not implemented
}
public static boolean hasAllOpts(byte resource) {
return true; //just a value, not implemented
}
} }
Any idea?任何的想法? I've been trying some我一直在尝试一些
It looks like you are just looking for set bits inside a byte (and for all set bits in that last method).看起来您只是在寻找一个字节内的设置位(以及最后一种方法中的所有设置位)。 To do this you can simply use your flags as a mask and compare to the flag.为此,您可以简单地使用您的标志作为掩码并与标志进行比较。
public class ResourceUtil {
public static boolean hasPublicSecurity(byte resource) {
return resource & FLAG_PUBLIC_SECURITY == FLAG_PUBLIC_SECURITY;
}
public static boolean hasPrivateSecurity(byte resource) {
return resource & FLAG_PRIVATE_SECURITY == FLAG_PRIVATE_SECURITY;
}
public static boolean hasBasicLifeSupport(byte resource) {
return resource & FLAG_BASIC_LIFE_SUPPORT == FLAG_BASIC_LIFE_SUPPORT;
}
public static boolean hasVolunteers(byte resource) {
return resource & FLAG_VOLUNTEERS == FLAG_VOLUNTEERS;
}
public static boolean hasAllOpts(byte resource) {
return resource & FLAG_ALL_OPTS == FLAG_ALL_OPTS;
}
}
NOTE: It's not completely clear to me what the getFlags
method is supposed to accomplish, but these checks should work注意:我不完全清楚getFlags
方法应该完成什么,但这些检查应该有效
The best (and probably fastest in terms of performance) way to do it would be to create a logical AND operation between the resources that you receive and the location of the bit.最好的(并且在性能方面可能是最快的)方法是在您收到的资源和位的位置之间创建一个逻辑 AND 操作。
So something like this:所以像这样:
public static boolean hasPublicSecurity(byte resource) {
return (resource & FLAG_PUBLIC_SECURITY) == FLAG_PUBLIC_SECURITY;
}
public static boolean hasPrivateSecurity(byte resource) {
return (resource & FLAG_PRIVATE_SECURITY) = FLAG_PRIVATE_SECURITY;
}
public static boolean hasBasicLifeSupport(byte resource) {
return (resource & FLAG_BASIC_LIFE_SUPPORT) == FLAG_BASIC_LIFE_SUPPORT;
}
public static boolean hasVolunteers(byte resource) {
return (resource & FLAG_VOLUNTEERS) == FLAG_VOLUNTEERS;
}
public static boolean hasAllOpts(byte resource) {
return (resource & FLAG_ALL_OPTS) == FLAG_ALL_OPTS;
}
The reason for the code is as follows, let's assume in your example that we have the value 7 ( 111
in binary), when we check for PUBLIC_SECURITY
we're basically checking if:代码的原因如下,假设在您的示例中我们的值为 7(二进制为111
),当我们检查PUBLIC_SECURITY
时,我们基本上是在检查:
111 & 001 == 001
If the result is zero, that means that the relevant bit was not 1.如果结果为零,则意味着相关位不是 1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.