逆向或算法?
我先描述一下我的情况。
我有一个十六进制值列表,称为 BaseID。它们的选择使得任意数量的它们之间的逻辑“或”将为您提供一个唯一的ID,称为FinalID。也就是说,我的BaseID值如下。
BaseID = { 0x01, 0x02, 0x04, 0x08, 0x10, ... }
关键点是我不知道在运行程序之前我将拥有多少个 BaseID(我确实得到当我运行程序时,它是从文件中读取的 BaseID 列表),或者 BaseID 总数中有多少将用于创建最终ID。这是根据我的程序的其他部分决定的。例如,以下是我的程序中可能有的两个 FinalID。
FinalID = ( 0x01 | 0x04 ) = 0x05
FinalID = ( 0x02 | 0x04 | 0x10 ) = 0x16
现在,对任意数量的 BaseID 使用 OR 运算是很简单的部分。我的问题是,我需要从 FinalID 中提取用于创建 FinalID 的 BaseID。由于我选择了 BaseID,一旦使用 OR 运算,它们将始终为我提供唯一的 FinalID,因此我知道任何给定的 FinalID 都是使用一组特定的 BaseID 创建的。请注意,也可以仅使用一个 BaseID 创建 FinalID,这相当于 FinalID = ( BaseID | 0x00 )。
我知道我必须做什么来提取用于创建任何特定 FinalID 的 BaseID;我必须获取所涉及的 BaseID 的完整列表,然后在每个元素组合之间使用 OR 运算符来找出哪个组合是否给出了特定的 FinalID。
然而我发现很难将这种逻辑转换成程序。我正在使用 C# 和 .NET 3.5 Framework。任何建议/想法将非常感激。代码示例受到高度赞赏。
提前致谢!
Let me first describe my situation.
I have a list of Hex values, which are called BaseID. They are chosen such that logical OR between any number of them will give you a unique ID which is called FinalID. That is, my BaseID values are as follows.
BaseID = { 0x01, 0x02, 0x04, 0x08, 0x10, ... }
The critical point is that I don't know how many BaseIDs that I will have before I run the program (I do get a list of BaseIDs once I run the program as it is read from a file), or how many out of the total number of the BaseIDs will be used to create the FinalID. It is decided based upon other parts of my program. For example, following are two FinalIDs that I might have in my program.
FinalID = ( 0x01 | 0x04 ) = 0x05
FinalID = ( 0x02 | 0x04 | 0x10 ) = 0x16
Now, using OR operation for any number of BaseIDs is the easy part. My problem is, I need to extract from FinalID the BaseIDs used to create that FinalID. Since I've chosen BaseIDs such that they, once the OR operation is used, will ALWAYS give me a unique FinalID, I know that any given FinalID is created using a specific set of BaseIDs. Note that a FinalID can be created using only one BaseID too which is the equivalent of FinalID = ( BaseID | 0x00 ).
I know what I have to do to extract the BaseIDs used to create any particular FinalID; I have to get the complete list of BaseIDs involved, then use OR operator among each and every comibnation of elements to find out if which combination gives the particular FinalID.
However I'm finding it hard to convert this logic into a program. I am using C# with .NET 3.5 Framework. Any suggestions/ideas would be very much appreciated. A code sample is highly appreciated.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好的,所以你已经有了一个位掩码...你需要做的就是将你的 FinalID 与每个可能的 BaseID 进行“AND” - 如果结果非零,则该 BaseID 会影响 FinalID:
我还建议你可以想要为此使用“flags”枚举,这将使您的语言集成方面的生活更简单。
Okay, so you've got a bitmask... all you need to do is "AND" your FinalID with each possible BaseID - if the result is non-zero, that BaseID contributed to the FinalID:
I would also suggest that you might want to use a "flags" enum for this, which will make your life simpler in terms of language integration.
这是我的建议:
Here is my suggestion:
让我试试...
应该不错。
Let me try...
Should do nice.
如果您有所有 BaseID 的列表,那么与 FinalID 执行 AND 操作应该会告诉您它是否已被使用。
逻辑
If you have the list of all BaseIDs, then doing an AND with FinalID should tell you whether it was used or not.
Logic
我将补充一点,使用
int
最多可以有 32 个 baseId,使用long
最多可以有 64 个 baseId。理论上,使用 BigInteger 类您可以达到您想要的高度。I'll add that using
int
you can have up to 32 baseId and usinglong
you can have up to 64 baseId. Using theBigInteger
class theorically you could go as high as you want.