Solved

# SHIFT,OR and AND operators in modula-2

Posted on 2004-11-18
286 Views
Dear experts,

I use Native XDS-x86 modula-2 compiler.

I need to use the bitwise left and right shift and witwise OR and AND operations on cardinal values.

what are the operators and and definitions modules.

Latha
0
Question by:slathakamatchi

LVL 100

Expert Comment

I am not a modula programmer but in checking the Modula language I don't find that it has those operators.     Since the Native compiler allows the use of C code could you be looking for the C operators.

mlmcc
0

LVL 15

Accepted Solution

mlmoc is qute right, the operators are not there in the vasic language.

Instead modula have the idea of sets, and you can get the bitwise logic done using sets:

var bitNumber1, bitNumber2, resultat :
RECORD
CASE t : BOOLEAN OF
TRUE:   nr: INTEGER
FALSE: bs: SET OF [0..30];   (* assuming INTEGER is 32 bit signed *)
END
END

bitNumber1.t := TRUE;
bitNumber1.nr := 121;                 (* 00000000 00000000 00000000 01111001 *)
bitNumber2.t := TRUE;
bitNumber2.nr := 195;                 (* 00000000 00000000 00000000 11000011 *)

bitNumber1.t := FALSE;
bitNumber1.t := FALSE;
resultat.t := FALSE;
resultat.bs := bitNumber1.bs + bitNumber2.bs             (* set union == bitwise or *)
resultat.t := TRUE;
writeln( "OR gives " +resultat.nr );   (* 00000000 00000000 00000000 11111011 (251) *)

resultat.t := FALSE;
resultat.bs := bitNumber1.bs * bitNumber2.bs             (* set intersection ==  bitwise and *)
resultat.t := TRUE;
writeln( "AND gives " +resultat.nr ); (* 00000000 00000000 00000000 01000001 (65) *)

for the shifting you multiply or divide by some power of 2

bitNumber1.t := TRUE;
resultat.nr := bitNumber1.nr * (2*2*2);                 (* shift up 3 bitpositions *)
(* 00000000 00000000 00000011 11001000 *)
resultat.nr := bitNumber1.nr / (2*2*2);                 (* shift down 3 bitpositions (NB signed shift)*)
(* 00000000 00000000 00000000 00001111 *)

It is clumsy but it works.  Still I would recommend you rewiew your prigramming task considering sets. there may be a much prettier solution.

regards JakobA
0

Author Comment

Hi Jacob,

Let me try this and let you know soon.

Thank you
0

## Featured Post

### Suggested Solutions

If you're not part of the solution, you're part of the problem.   Tips on how to secure IoT devices, even the dumbest ones, so they can't be used as part of a DDoS botnet.  Use PRTG Network Monitor as one of the building blocks, to detect unusual…
PRTG Network Monitor lets you monitor your bandwidth usage, so you know who is using up your bandwidth, and what they're using it for.
Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…