如何用单个值表示 4 种布尔可能性
我想在一个值中存储 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
位标志。
Bit flags.
确实可以这样做,但不建议这样做。
如果您想在数据库中查找所有单身学生该怎么办?还是男的?
虽然您节省的空间很小,但您放弃了对数据进行索引以进行更快搜索的可能性,不仅如此,而且要查找具有取决于这些属性中的任何一个查询条件的任何数据,您将需要执行位移操作对所有记录(或检查多个整数值)这最终会非常慢。
以我的拙见,增加的复杂性并没有真正产生太大的好处,数据库可以处理 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).
您可能会发现 EnumSet 是表示此信息的自然方式。 EnumSet 在底层使用位掩码。
您可以添加任意数量的枚举值。
You may find that an EnumSet is a natural way to represent this information. An EnumSet uses a bit mask under the bonnet.
You can add any number of enum values.
使用二进制打包。将它们定义为常量:
并用逻辑或运算将它们组合起来。这样,每种可能性都将由数字的二进制表示形式中的一位来表示。您可以通过将打包值与任何常量进行 AND 运算来进行检查:
Use binary packing. Define constants for them as:
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:
您是否考虑过使用 Byte 来存储值?然后,您可以执行如下操作:
然后,您可以使用如下代码:
来获取详细信息。
...也就是说,为了三个字节,值得所有额外的工作和常量吗?
Have you considered using a Byte to store the values? You could then do something like the following:
Then later you can use code like:
to get the details.
... That said, for the sake of three bytes, is it worth all the extra work and constants?
最好的方法是使用
boolean[4]
,您可以将每个值保存在数组的不同单元格中,并且占用的空间最小!在您的班级中编写这段代码:
现在只需使用以下代码即可:
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:
now just use this like:
setValue("IsGraduate", true);
getValue("IsMale");
如果您只是为了好玩而使用该解决方案,那没问题,但如果这是专业应用程序,请不要在数据库中的单个参数中实现它。如果您这样做,请考虑业务的变化:
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:
您确实应该使用单独的变量来存储这些。一个人可以是毕业生、单身、男性,正在寻找伴侣。您根本无法将所有这些可能性存储在一个布尔值中。
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.