使用以 2 为基数的数字对唯一选项列表进行编码的技术有名称吗?

发布于 2024-09-03 17:34:01 字数 258 浏览 2 评论 0原文

我正在编写几个函数,将选项列表编码和解码为 Long,以便它们可以轻松地在应用程序中传递,您知道这种事情:

1 - 苹果
2 - 橙色
4 - 香蕉
8 - 梅子
等等

在这种情况下,数字 11 代表 Apple、Orange 和 Apple。梅子。

我已经让它工作了,但我看到它一直在使用,所以假设该技术有一个通用名称,毫无疑问,目前我无法企及的各种最佳实践和聪明的算法。

I am writing a couple of functions that encode and decode a list of options into a Long so they can easily be passed around the application, you know this kind of thing:

1 - Apple
2 - Orange
4 - Banana
8 - Plum
etc.

In this case the number 11 would represent Apple, Orange & Plum.

I've got it working but I see this used all the time so assume there is a common name for the technique, and no doubt all sorts of best practice and clever algorithms that are at the moment just out of my reach.

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

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

发布评论

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

评论(4

双马尾 2024-09-10 17:34:01

位标志。这是一种用作位掩码一部分的技术。

0001 - 苹果
0010 - 橙子
0100 - 香蕉
1000 - Plum

每个 1 都是标记位。

现在您可以使用这些数字轻松执行按位运算:

if((11 & Apple) == Apple) // The Apple Flag is set
{
    // Do Something
}

Bit Flags. It's a technique used as part of Bitmasking.

0001 - Apple
0010 - Oranage
0100 - Banana
1000 - Plum

Each 1 is the flagged bit.

Now you can easily perform bitwise operations using those number:

if((11 & Apple) == Apple) // The Apple Flag is set
{
    // Do Something
}
春风十里 2024-09-10 17:34:01

标志


 
 
 
 

Bitflags

 
 
 
 
 

﹏半生如梦愿梦如真 2024-09-10 17:34:01

通过 c# Flags 属性 我将使用一个位字段或

一组相关的标志,在硬件中还有 one-hot 编码,尽管这意味着您没有获得标志集的组合

going by the help for the c# Flags attribute i'm going to go with a bit field or set of flags

sort of related, in hardware there is also one-hot encoding though this implies you don't get combinations of flags set

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