如何用单个值表示 4 种布尔可能性

发布于 2024-10-21 18:18:23 字数 287 浏览 2 评论 0原文

我想在一个值中存储 4 个布尔可能性。例如, 我想要一个单一值来判断一个人是否:

IsSingle
已毕业
是男
IsLookingForPartner

那么将它们存储在一个字节中是否合适,可以分配 4 位,每个参数一个。如果该位设置为 1,则该布尔参数为 true。所以我可以进行位移操作来找出所有内容都是正确的。如果字节的值为 111,则前三个参数为 true。这是个好办法吗?有没有更好的方法来实现这个?

我需要将该值作为单个参数存储在数据库中。

I want to store 4 boolean possibilities in a single value. For example,
I want a single value that tells whether a person is:

IsSingle
IsGraduate
IsMale
IsLookingForPartner

So is it good to store them in a byte, whose 4 bits may be allocated, one for each parameter. If the bit is set to 1 then that boolean parameter is true. So I can do bit shift operations to find what all are true. if value of byte is 111 then first three parameters are true. Is it a good way? Is there a better way to implement this?

I need to store this value as a single parameter in the database.

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

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

发布评论

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

评论(8

¢蛋碎的人ぎ生 2024-10-28 18:18:23

位标志。

public static final int IsSingle = 1 << 0;
public static final int IsGraduate = 1 << 1;
public static final int IsMale = 1 << 2;
public static final int IsLookingForPartner = 1 << 3;

...

if ((Value & IsSingle) != 0) {
}

if ((Value & IsGraduate) != 0) {
}

// Set "Single"
Value |= IsSingle;

// Remove "Graduate"
Value &= ~IsGraduate;

Bit flags.

public static final int IsSingle = 1 << 0;
public static final int IsGraduate = 1 << 1;
public static final int IsMale = 1 << 2;
public static final int IsLookingForPartner = 1 << 3;

...

if ((Value & IsSingle) != 0) {
}

if ((Value & IsGraduate) != 0) {
}

// Set "Single"
Value |= IsSingle;

// Remove "Graduate"
Value &= ~IsGraduate;
亢潮 2024-10-28 18:18:23

确实可以这样做,但不建议这样做。

如果您想在数据库中查找所有单身学生该怎么办?还是男的?
虽然您节省的空间很小,但您放弃了对数据进行索引以进行更快搜索的可能性,不仅如此,而且要查找具有取决于这些属性中的任何一个查询条件的任何数据,您将需要执行位移操作对所有记录(或检查多个整数值)这最终会非常慢。

以我的拙见,增加的复杂性并没有真正产生太大的好处,数据库可以处理 4 个独立的字段,并且有充分的理由这样做(例如轻松查询这些字段上的数据)。

It is indeed possible to do this, however it is not recommended.

What if you wanted to find in your database all the students who are single? Or Male?
While the space you are saving is minimal, you are giving up the possibility of indexing your data for faster search not only that, but to find any data which has query criteria depending on any of these attributes, you would need to perform bit shifting operations on all the records (or checking against multiple integer values) which would end up being terribly slow.

The added complexity doesn't really yield much of a benefit in my humble opinion, databases can handle 4 separate fields and there is good reason for doing it that way (e.g querying your data on those fields easily).

话少心凉 2024-10-28 18:18:23

您可能会发现 EnumSet 是表示此信息的自然方式。 EnumSet 在底层使用位掩码。

enum Interest {
   IsSingle,
   IsGraduate,
   IsMale,
   IsLookingForPartner 
}

EnumSet<Interest> interests = EnumSet.of(Interest.IsSingle, Interest.IsMale);

您可以添加任意数量的枚举值。

You may find that an EnumSet is a natural way to represent this information. An EnumSet uses a bit mask under the bonnet.

enum Interest {
   IsSingle,
   IsGraduate,
   IsMale,
   IsLookingForPartner 
}

EnumSet<Interest> interests = EnumSet.of(Interest.IsSingle, Interest.IsMale);

You can add any number of enum values.

遗心遗梦遗幸福 2024-10-28 18:18:23

使用二进制打包。将它们定义为常量:

IsSingle = 1;
IsGraduate = 2;
IsMale = 4;
IsLookingForPartner = 8;

并用逻辑或运算将它们组合起来。这样,每种可能性都将由数字的二进制表示形式中的一位来表示。您可以通过将打包值与任何常量进行 AND 运算来进行检查:

short status = IsSingle | IsMale;    // will give 5

if ((status & IsMale) != 0)
    System.out.println("Male.");

Use binary packing. Define constants for them as:

IsSingle = 1;
IsGraduate = 2;
IsMale = 4;
IsLookingForPartner = 8;

And combine them with logical OR operation. That way each possibility will be represented by a bit in the binary representation of a number.You can check by AND-ing the packed value with any of the constants:

short status = IsSingle | IsMale;    // will give 5

if ((status & IsMale) != 0)
    System.out.println("Male.");
陌上芳菲 2024-10-28 18:18:23

您是否考虑过使用 Byte 来存储值?然后,您可以执行如下操作:

public Byte setDetails(bool isSingle, bool isGraduate, bool isMale, bool isLooking)
{
    Byte result = 0;

    //Set the details
    if (isSingle) result = result | 1;
    if (isGraduate) result = result | 2;
    if (isMale) result = result | 4;
    if (isLooking) result = result | 8;

    return result;
}

然后,您可以使用如下代码:

Byte details = getDetails(true, false, true, false);
bool isSingle = (details & 1) != 0;

来获取详细信息。

...也就是说,为了三个字节,值得所有额外的工作和常量吗?

Have you considered using a Byte to store the values? You could then do something like the following:

public Byte setDetails(bool isSingle, bool isGraduate, bool isMale, bool isLooking)
{
    Byte result = 0;

    //Set the details
    if (isSingle) result = result | 1;
    if (isGraduate) result = result | 2;
    if (isMale) result = result | 4;
    if (isLooking) result = result | 8;

    return result;
}

Then later you can use code like:

Byte details = getDetails(true, false, true, false);
bool isSingle = (details & 1) != 0;

to get the details.

... That said, for the sake of three bytes, is it worth all the extra work and constants?

剩余の解释 2024-10-28 18:18:23

最好的方法是使用 boolean[4],您可以将每个值保存在数组的不同单元格中,并且占用的空间最小!

在您的班级中编写这段代码:

private boolean[] values = new boolean[4];

void setValue(String name, boolean value){
    if(name=="IsSingle")
        values[0] = value;
    else if(name=="IsGraduate")
        values[1] = value;
    else if(name=="IsMale")
        values[2] = value;
    else if(name=="IsLookingForPartner")
        values[3] = value;
}

boolean getValue(String name){
    if(name=="IsSingle")
        return values[0];
    else if(name=="IsGraduate")
        return values[1];
    else if(name=="IsMale")
        return values[2];
    else if(name=="IsLookingForPartner")
        return values[3];
    else
        return false;
}

现在只需使用以下代码即可: setValue("IsGraduate", true); getValue("IsMale");

The best would be an boolean[4] where you will save each value in different cell of array, and it takes minimum space!

write this piece of code in your class:

private boolean[] values = new boolean[4];

void setValue(String name, boolean value){
    if(name=="IsSingle")
        values[0] = value;
    else if(name=="IsGraduate")
        values[1] = value;
    else if(name=="IsMale")
        values[2] = value;
    else if(name=="IsLookingForPartner")
        values[3] = value;
}

boolean getValue(String name){
    if(name=="IsSingle")
        return values[0];
    else if(name=="IsGraduate")
        return values[1];
    else if(name=="IsMale")
        return values[2];
    else if(name=="IsLookingForPartner")
        return values[3];
    else
        return false;
}

now just use this like: setValue("IsGraduate", true); getValue("IsMale");

风尘浪孓 2024-10-28 18:18:23

如果您只是为了好玩而使用该解决方案,那没问题,但如果这是专业应用程序,请不要在数据库中的单个参数中实现它。如果您这样做,请考虑业务的变化:

  1. 删除 isLookingForPartner // 冗余位将一直存在 - 顺序很重要
  2. 添加新参数
  3. 查找所有 isLookingForPartner==true // 会很慢

If you want to use the solution just for fun that's OK but if this will be professional application DO NOT implement this in single parameter in database. If you do that think about business' changes:

  1. Remove isLookingForPartner //redundant bit will be all the time - order does matter
  2. Add new parameters
  3. Find all persons who are isLookingForPartner==true //will be slow
口干舌燥 2024-10-28 18:18:23

您确实应该使用单独的变量来存储这些。一个人可以是毕业生、单身、男性,正在寻找伴侣。您根本无法将所有这些可能性存储在一个布尔值中。

You really should use separate variables to store these. A person could be a graduate, single, male, and looking for a partner. You simply can't store all those possibilities in a single boolean.

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