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=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,ee7e80feb46de7ac X-Google-Attributes: gid103376,public From: mheaney@ni.net (Matthew Heaney) Subject: Re: How to perform bit-wise xor, and...? Date: 1996/11/22 Message-ID: #1/1 X-Deja-AN: 198215801 distribution: inet references: <5747oa$svi@umacss1.umac.mo> content-type: text/plain; charset=ISO-8859-1 organization: Estormza Software mime-version: 1.0 newsgroups: comp.lang.ada Date: 1996-11-22T00:00:00+00:00 List-Id: In article <5747oa$svi@umacss1.umac.mo>, d941686@sp2 (Leong San Io Francisco) wrote: > Can someone teach me how to perform bit-wise "xor", and, "or" of two >integers in Ada so that it can generate code as close as to the underlying >computer's instructions like the C's ^, &, | operators? I am trying to write >some computer instructions emulation stuff as well as bitmap graphics >manipulation which requires "xor", "or" bytes. Is multiplication by 2 really >equal to shift left and division by integer 2 to shift right? > My Ada compiler only provides xor, and operations for booleans and >not integers... The following comments apply equally to Ada 83 and Ada 95. Many people don't realize that in Ada, type Boolean, and arrays of type Boolean, have special semantics. The Ada standard states that Boolean'Size is 1. The Ada standard states that if you pragma Pack an array whose component subtype has a size of 1, then the array type is guaranteed to have its components in contiguous bits. That means you can pragma Pack an array of Boolean, and each element of the array will occupy contiguous bits of storage. The Ada standard also states that an array of Boolean also comes with the predefined operators "or", "and", and "xor". This is not true for other array types, whose component subtype is not Boolean. So, it you want to exclusive-or a pair of integers, then you can write a function to do so, as follows: type T is range 0 .. 100; -- say function "xor" (Left, Right : T) return Boolean; function "or" (Left, Right : T) return Boolean; function "and" (Left, Right : T) return Boolean; ... subtype Boolean_Array_Range is Positive range 1 .. T'Size; type Boolean_Array is array (Boolean_Array_Range) of Boolean; pragma Pack (Boolean_Array); function To_Boolean_Array is new Unchecked_Conversion (T, Boolean_Array); function To_T is new Unchecked_Conversion (Boolean_Array, T); function "xor" (Left, Right : T) return Boolean is Left_Array : constant Boolean_Array := To_Boolean_Array (Left); Right_Array : constant Boolean_Array := To_Boolean_Array (Right); The_Return_Value_Array : constant Boolean_Array := Left_Array xor Right_Array; The_Return_Value : constant T := To_T (The_Return_Value_Array); begin return The_Return_Value; end; The other operations follow similarly. I said all this so that readers will realize the Boolean arrays really do have special properties. Many of my clients don't realize this. That being said, perhaps exclusive-or'ing objects of type integer isn't really what you want to do. Many other languages (C, for example) that lack Ada's rich typing features allow you to exclusive-or integers, but only because there is no alternative. Doing bit manipulation is a pretty low-level thing. This is perfectly reasonable if you have a low-level thing to do, such as I/O to a hardware device. If that's the case, why not use a low-level type? If you want to manipulate bits, then why not manipulate an object whose type is an array of bits? Perhaps you're trying to get at a bit field in the integer. If that is the case, then a record with a suitable representation clause is really what you want. Note that in Ada 95, modular types come with predefined operations that do what you require ("xor", etc). So perhaps a modular type is what you want, instead of an integer type. If you are doing bit manipulation because you are interfacing with hardware, then maybe you should probably be using the modular types declared in the (Ada 95) predefined package Interfaces. The types in that package also come with operations to do bit shift and rotation. Information about package Interfaces is in Section B.2, RM95. The bottom line is, use the right type for the job. Integers weren't made to do bit manipulation, and that's why they don't come with predefined exclusive-or operations. -------------------------------------------------------------------- Matthew Heaney Software Development Consultant mheaney@ni.net (818) 985-1271