Learn how to a build a cloud-first strategyRegister Now


SHIFT,OR and AND operators in modula-2

Posted on 2004-11-18
Medium Priority
Last Modified: 2010-04-16
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.

Question by:slathakamatchi
LVL 101

Expert Comment

ID: 12617654
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.

LVL 15

Accepted Solution

JakobA earned 300 total points
ID: 12640048
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 :
                            CASE t : BOOLEAN OF
                                TRUE:   nr: INTEGER
                                 FALSE: bs: SET OF [0..30];   (* assuming INTEGER is 32 bit signed *)

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

Author Comment

ID: 12641571
Hi Jacob,

Let me try this and let you know soon.

Thank you

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Tech giants such as Amazon and Google have sold Alexa and Echo to such an extent that they have become household names. And soon they are expected to be used by commoners in their homes, ordering takeout, picking out a song, answering trivia questio…
Exchange administrators are always vigilant about Exchange crashes and disasters that are possible any time. It is quite essential to identify the symptoms of a possible Exchange issue and be prepared with a proper recovery plan. There are multiple…
Loops Section Overview
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question