SHIFT,OR and AND operators in modula-2

Posted on 2004-11-18
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 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.

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

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

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

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

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

    bitNumber1.t := TRUE; := * (2*2*2);                 (* shift up 3 bitpositions *)
                                                         (* 00000000 00000000 00000011 11001000 *) := / (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

    Hi Jacob,

    Let me try this and let you know soon.

    Thank you

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    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 (, 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…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now