Help - Search - Members - Calendar
Full Version: Bitwise operations
Modelica Forum > Modelica > Modelica
Djordjevic
Hi@all.

Is there a possibility of using bitwise operators and logical shifting in Modelica?
Roland
Hello Djordjevic,

short answer: No.
A little longer answer: No, but you can use external C functions for this.

For example, this will work:
CODE
function XOR
  input Integer x1;
  input Integer x2;
  output Integer y;
annotation (Include="
    int xor(int x1, int x2) {
      return x1^x2;
    }");
  external "C" y = xor(x1, x2);
end XOR;

model Tester
  Integer x=1;
  Integer y=3;
  Integer z = XOR(x, y);
end Tester;


I hope this helps a little bit.
Roland
Djordjevic
Thank you Roland.

I wanted to make many booleaan values out of one integer variable. So that e.g. input=6 would result the output false, true, true, false.

So the idea was to use a simple for-loop like in C:
CODE

for (i=0; i<4; i++)
  output[i] = (x & (0001)<<(i)));


Now that its not possible I wanted to try it with another for-loop such as. This time (hopefully) modelica:
CODE

for i in 3:0 loop
  if (input>2^i) then
    output[i] = true;
    input = input - 2^i;
  else
    output[i] = false;
  end if;
end for;


But I am experiencing some compiling errors, which I'm not used at C++.
Maybe Modellica still "thinks", that I am using an eqation like x=x+1, which is wrong, instead of interpreting it as x:=x+1. But that's only my guess.
I know the algorithm-part needs an algorothm keyword, which I also use, and the varables input and output are not the one that would physically come into the Model.
Of course, I could write the code 4-times for a 4-bit varable, but it would be very unpleasent and ugly to convert a 32-bit variable by copy-pasting.

Can you tell me what I'm doing wrong?
liuliu
Write a function in c or in modelica should solve this problem
try to give you a simple example. Be aware, the code listed below use outputValue[1] to represent the 2^3 and output[4] the 2^0. Of course you can change the order by your self.

CODE

function IntegerToBinary
  
input Real inputValue;
output Boolean outputValue[4];
protected
Real temp;
algorithm
temp:=inputValue;
for i in 1:4 loop
  if (temp>=2^(4-i)) then
    outputValue[i] :=true;
    temp:=temp - 2^(4 - i);
  else
    outputValue[i] :=false;
      
  end if;
end for;
  
end IntegerToBinary;




QUOTE(Djordjevic @ Nov 18 2008, 09:25 AM) *

Thank you Roland.

I wanted to make many booleaan values out of one integer variable. So that e.g. input=6 would result the output false, true, true, false.

So the idea was to use a simple for-loop like in C:
CODE

for (i=0; i<4; i++)
  output[i] = (x & (0001)<<(i)));


Now that its not possible I wanted to try it with another for-loop such as. This time (hopefully) modelica:
CODE

for i in 3:0 loop
  if (input>2^i) then
    output[i] = true;
    input = input - 2^i;
  else
    output[i] = false;
  end if;
end for;


But I am experiencing some compiling errors, which I'm not used at C++.
Maybe Modellica still "thinks", that I am using an eqation like x=x+1, which is wrong, instead of interpreting it as x:=x+1. But that's only my guess.
I know the algorithm-part needs an algorothm keyword, which I also use, and the varables input and output are not the one that would physically come into the Model.
Of course, I could write the code 4-times for a 4-bit varable, but it would be very unpleasent and ugly to convert a 32-bit variable by copy-pasting.

Can you tell me what I'm doing wrong?

Roland
Hello Djordjevic,

what is not correct in your code:
Arrays in Modelica start with the index 1 and not 0.
You have (probably) written equations - what you need to use is an algorithm as you also wrote. You need this because of
CODE
input := input - 2^i;

In the if statement you have to compare using '>=' instead of just '>'.

One suggestion would be:

CODE
model Test
  parameter Integer x = 6;
  parameter Integer nBytes = 4;
  Boolean y[nBytes];
  
protected
  Integer tmp;
  
algorithm
  tmp := x;
  for i in 1:nBytes loop
    if tmp >= 2^(nBytes - i) then
      y[nBytes - i + 1] := true;
      tmp := tmp - integer(2^(nBytes - i));
    else
      y[nBytes - i + 1] := false;
    end if;
  end for;
end Test;


Of course all numbers greater than 2^(nBytes - 1) will result in y[nBytes] set to true.

Roland
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.