From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.7 required=5.0 tests=BAYES_00,INVALID_DATE, MSGID_SHORT,REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10 5/3/83; site gsg.UUCP Path: utzoo!watmath!clyde!burl!ulysses!bellcore!decvax!gsg!vilot From: vilot@gsg.UUCP (Michael Vilot) Newsgroups: net.lang.ada Subject: Re: bit manipulation and logical operators Message-ID: <163@gsg.UUCP> Date: Wed, 26-Feb-86 09:16:41 EST Article-I.D.: gsg.163 Posted: Wed Feb 26 09:16:41 1986 Date-Received: Fri, 28-Feb-86 21:36:55 EST References: <306@telesoft.UUCP> Reply-To: vilot@gsg.UUCP (Michael Vilot) Organization: General Systems Group, Salem, NH List-Id: In article <306@telesoft.UUCP> bruceb@telesoft.UUCP (Bruce Bergman @favorite) writes: >I come from a C background (alright - no razzberries...) and as such >I have no fear of using bit and word logical operators like AND, OR, >and XOR. In Ada, this is a realizable fear. > >I may be missing some part of the language, and if I am, please tell >me, but how in the crap can I get the equivalent of AND, OR, and XOR >in Ada? Of course, it's hard to tell what you are trying to do without some idea of the application. A typical use of these operators is to use collections of bits as status variables, and to use the logical operators to selectively mask parts of words to get at the status information. In Ada, the intent is to deal with this issue on a more abstract level. While the data may be *stored* as a collection of bits packed into a word, each bit of status information *really* (the way you _think_ about it) is a Boolean indicator. In the more general case of, say 3 bits representing 8 states, this status information is an enumeration class. So, on implementations supporting representation specifications, consider the example given in the Ada Language Reference Manual, Chapter 13, Section 13.4, paragraph 9 [ 13.4(9) ] -- page 13-6: WORD : constant := 4; -- storage unit is byte, 4 bytes per word type STATE is (A, M, W, P); type MODE is (FIX, DEC, EXP, SIGNIF); type BYTE_MASK is array (0..7) of BOOLEAN; type STATE_MASK is array (STATE) of BOOLEAN; type MODE_MASK is array (MODE) of BOOLEAN; -- These type definitions allow a more abstract manipulation -- They can be mapped onto a physical implementation just once, and -- referenced by their logical names and properties type PROGRAM_STATUS_WORD is record SYSTEM_MASK : BYTE_MASK; PROTECTION_KEY : INTEGER range 0 .. 3; MACHINE_STATE : STATE_MASK; INTERRUPT_CAUSE : INTERRUPTION_CODE; ILC : INTEGER range 0 .. 3; CC : INTEGER range 0 .. 3; PROGRAM_MASK : MODE_MASK; INST_ADDRESS : ADDRESS; end record; -- The record representation clause lets you force the compiler to -- pack the information. See the rest of Chapter 13 for all the -- related cautions and warnings. for PROGRAM_STATUS_WORD use record at mod 8; SYSTEM_MASK at 0*WORD range 0 .. 7; PROTECTION_KEY at 0*WORD range 10 .. 11; -- bits 8,9 unused MACHINE_STATE at 0*WORD range 12 .. 15; INTERRUPT_CAUSE at 0*WORD range 16 .. 31; ILC at 1*WORD range 0 .. 1; -- second word CC at 1*WORD range 2 .. 3; PROGRAM_MASK at 1*WORD range 4 .. 7; INST_ADDRESS at 1*WORD range 8 .. 31; end record; for PROGRAM_STATUS_WORD'SIZE use 8*SYSTEM_STORAGE_UNIT; -- Now, this information can be manipulated * logically * : ... SOME_WORD : PROGRAM_STATUS_WORD; PART_MASK : constant STATE_MASK := (FALSE, FALSE, TRUE, FALSE); begin if SOME_WORD.PROGRAM_MASK(FIX) = TRUE then SOME_WORD.MACHINE_STATE := PART_MASK; else SOME_WORD.MACHINE_STATE(A) := FALSE; end if; ... -- and still have the * physical * bits turn out the way the -- machine expects. -- Michael J. Vilot decvax!gsg!vilot (UUCP) General Systems Group vilot@wang-inst (CSNET) 51 Main Street MVilot@USC-ISIF (ARPA) Salem, NH 03079 (603) 893-1000 (DDD)