允许操作符在位向量的位之间任意使用 [英] Allowing operator to be used arbitrarily between bits of a bitvector

查看:78
本文介绍了允许操作符在位向量的位之间任意使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C的新手。使用C作为工具的前端语言。



我想定义一个运算符数组:And,Or,Not 。使用这些运算符或从这些基本运算符构建复杂运算符,并允许任何运算符在10位长位向量之间使用。



类似于:



I'm new to C. Using C as front end language for a tool.

I want to define an array of operators : And , Or , Not. Use these Operator or build complex operator from these basic ones and allow any operator to be used between 10 bits long bitvector.

Something like :

alphaType array[3] = { And , Or , Not};
bitvector vect  = 0b1011011001;     // Any size 10 bitvector



允许来自数组的任何运算符将在vect中的两个位之间使用。例如,假设x1..x10变量表示位。




Allowing any operator from array to be used between two bits in the vect. For example lets say x1..x10 variable represents the bits.

x1 And (Not x2) Or x3 And x4 ....



或者像Xor那样从这些运算符中创建运算符然后再使用它。例如




Or make an operator from these like Xor and then use that too. for example

Xor : x1 Xor x2 Xor x3 .....





两个问题:



如何在C中指定允许任意操作员在位之间使用。



如何指定构建复杂运算符形式的基本运算符并使用它做1.



有没有办法在C中实现相同的目的??谢谢



Two questions :

How to specify in C to allow arbitrary operator use between bits.

How to specify building complex operator form basic ones and using that doing 1.

Is there any way to achieve the same in C ?? Thanks

推荐答案

你的问题有几种方法都具有不同的通用性,实现起来或多或少都很复杂。



(1)您可以为
There are several approaches to your problem which all have different degrees of generality and which are more or less complicated to implement.

(1) You could write an interpreter for expressions like
x1 And (Not x2) Or x3 And x4

这样的表达式编写解释器,它解析字符串并执行相应的你的位向量上的操作。



(2)你可以编写一个解析字符串的类似解释器,但是生成可以执行任意操作序列的中间代码bitvector,可以多次使用而无需重新解析输入字符串。



(3)如果您可以将表达式指定为代码的一部分,你可以编写一组名为X1 .... X10的简单宏,它从位向量中提取相应的位。然后你写代码如

which parses a string and performs the respective operations on your bit vector.

(2) You could write a similar interpreter which parses the string, but instead generates intermediate code that can perform the operations sequence on any arbitrary bitvector and which could be used multiple times without having to re-parse the input string.

(3) If you can live with specifying the expression as part of the code, you could write a simple set of macros called X1 .... X10, which extract the respective bit from the bit vector. Then you write code like

BOOL result = X1 && (!X2) || X3;





您还应该考虑如何表示位向量。在C ++中,您可以使用其中一个预定义的类,但在C语言中,您几乎可以使用它们。你最多有32位变量吗?然后你可以简单地使用int或long。对于任意长度的位向量,您需要实现一组函数,这些函数可用于整数或长整数(或简称为无符号字符)。你肯定会在互联网上找到一些代码,但你自己写的也不是很难。



所以,决定你想要追求的方式如果您对细节有任何疑问,请不要犹豫。



You should also think about how you want to represent the bit vector. In C++ you could use one of the pre-defined classes, but in C you are pretty much on your own. Will you have at most 32 bit variables? Then you could simply use an int or a long. For bit vectors of arbitrary length you need to implement a set of functions that work on arrays of ints or longs (or simply unsigned chars). You will certainly find some code for that in the internet, but it is also not very difficult to write that yourself.

So, decide on which way you want to pursue and if you have any more questions on the details, don't hesitate to ask.


这篇关于允许操作符在位向量的位之间任意使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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