为什么要使用标志+位掩码,而不是一系列的布尔值? [英] Why use flags+bitmasks rather than a series of booleans?

查看:271
本文介绍了为什么要使用标志+位掩码,而不是一系列的布尔值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于在那里我有一个对象,可以是一个或多个真/假状态的情况下,我一直在为什么程序员经常使用,而不是只用几个布尔值标志+位掩码有点模糊。

Given a case where I have an object that may be in one or more true/false states, I've always been a little fuzzy on why programmers frequently use flags+bitmasks instead of just using several boolean values.

这一切都在.NET框架。不知道这就是最好的例子,但在.NET框架有以下内容:

It's all over the .NET framework. Not sure if this is the best example, but the .NET framework has the following:

public enum AnchorStyles
{
    None = 0,
    Top = 1,
    Bottom = 2,
    Left = 4,
    Right = 8
}

所以,给出一个锚的风格,我们可以用位掩码来找出哪些国家的选择。但是,好像你可以完成同样的事情与AnchorStyle类/结构为每个可能的值,或单个枚举值数组定义布尔属性。

So given an anchor style, we can use bitmasks to figure out which of the states are selected. However, it seems like you could accomplish the same thing with an AnchorStyle class/struct with bool properties defined for each possible value, or an array of individual enum values.

中当然,我的问题的主要原因是,我不知道我是否应该遵循自己的代码有类似的做法。

Of course the main reason for my question is that I'm wondering if I should follow a similar practice with my own code.

那么,为什么用这个办法?

So, why use this approach?


  • 更少的内存消耗? (它没有的看起来的像它会比布尔变量数组/结构消耗更少)

  • 更好的堆栈/堆不是一个结构或阵列的性能?

  • 更快的比较操作?更快的价值添加/删除?

  • 更方便了谁写的开发?

  • Less memory consumption? (it doesn't seem like it would consume less than an array/struct of bools)
  • Better stack/heap performance than a struct or array?
  • Faster compare operations? Faster value addition/removal?
  • More convenient for the developer who wrote it?

推荐答案

这是传统上减少内存使用的方法。所以,是的,它用C比较陈旧#: - )

It was traditionally a way of reducing memory usage. So, yes, its quite obsolete in C# :-)

作为一种编程技术,它可能在当今的系统过时了,你会是相当好的使用一个布尔变量的数组,但是...

As a programming technique, it may be obsolete in today's systems, and you'd be quite alright to use an array of bools, but...

它是快速比较存储为一个位掩码值。使用AND和OR逻辑运算和比较产生的2个整数。

It is fast to compare values stored as a bitmask. Use the AND and OR logic operators and compare the resulting 2 ints.

它使用相当少的内存。把你的范例值的所有4位掩码会用半个字节。使用的bool的阵列,最有可能的,就可以使用几个字节的数组对象加一个长字的每个布尔。如果你有存储一百万的价值观,你会看到究竟为什么一个位掩码版本为优。

It uses considerably less memory. Putting all 4 of your example values in a bitmask would use half a byte. Using an array of bools, most likely would use a few bytes for the array object plus a long word for each bool. If you have to store a million values, you'll see exactly why a bitmask version is superior.

这是比较容易管理的,你只需要处理一个整数值,而布尔变量的阵列将存储在完全不同的,说的数据库中。

It is easier to manage, you only have to deal with a single integer value, whereas an array of bools would store quite differently in, say a database.

而且,由于存储器布局的,在比阵列每一个方面快得多。这几乎是使用单一的32位整数一样快。我们都知道这是一样快,你可以得到的数据操作。

And, because of the memory layout, much faster in every aspect than an array. It's nearly as fast as using a single 32-bit integer. We all know that is as fast as you can get for operations on data.

这篇关于为什么要使用标志+位掩码,而不是一系列的布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆