Click here to Skip to main content
15,902,777 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
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 :

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

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

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

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

C++
Xor : x1 Xor x2 Xor x3 .....


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
Posted
Updated 27-Dec-15 22:22pm
v3
Comments
Sergey Alexandrovich Kryukov 28-Dec-15 0:04am    
You need to explain the idea, what you really want to achieve.
Don't you think your array needs to include '(' and ')', otherwise you cannot implement comprehensive set of operations?
And what's the problem? Creation of an array? But this is trivial. Carrying out operations? Read on the bitwise operators.
—SA
ALEX_CROME 28-Dec-15 0:35am    
This is trivial if you know the operations you want to carry out. Can you allow any arbitrary combinations ? The main goal is to check all possible combinations.
For example :
x1 | x2 & x3 ... is one combination
x1 & x3 & x4 .. is another combination..

Doinng this i can achieve the effect of all boolean functions possible. Don't pass judgement if you don't understand what is asked.
Richard MacCutchan 28-Dec-15 4:25am    
The question is not C specific. The issue is that you first have to define the rules for all the operations that you require. Only then can you write the C code to implement it, which is the easy bit.

1 solution

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
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
C++
BOOL result = X1 && (!X2) || X3;


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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900