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.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,70ff6d6e203062f6 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-12-22 19:33:11 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!newshub.sdsu.edu!elnk-nf2-pas!newsfeed.earthlink.net!attbi_feed3!attbi.com!attbi_s52.POSTED!not-for-mail From: "Steve" Newsgroups: comp.lang.ada References: Subject: Re: Newbie question: How does one do bit manipulation in Ada? X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 Message-ID: NNTP-Posting-Host: 12.211.58.135 X-Complaints-To: abuse@comcast.net X-Trace: attbi_s52 1072150390 12.211.58.135 (Tue, 23 Dec 2003 03:33:10 GMT) NNTP-Posting-Date: Tue, 23 Dec 2003 03:33:10 GMT Organization: Comcast Online Date: Tue, 23 Dec 2003 03:33:10 GMT Xref: archiver1.google.com comp.lang.ada:3733 Date: 2003-12-23T03:33:10+00:00 List-Id: Ada only permits bit masking on arrays of booleans or modular types. To do an XOR on integers convert the integers to modular types using unchecked conversion, xor the resulting modular values, and convert the result back to integer. This question came up recently, here's the sample program I posted earlier. with Ada.Integer_Text_IO; with Ada.Text_IO; with Ada.Unchecked_Conversion; procedure DoIntXOR is function "xor"(Left,Right : in Integer) return Integer is pragma Inline("xor"); type Int_As_Mod is mod 2 ** Integer'Size; function To_Mod is new Ada.Unchecked_Conversion( Integer, Int_As_Mod ); function To_Int is new Ada.Unchecked_Conversion( Int_As_Mod, Integer ); begin return To_Int( To_Mod(Left) xor To_Mod(Right) ); end "xor"; a_value : Integer; b_value : Integer; begin Ada.Text_IO.Put( "Enter first value > " ); Ada.Integer_Text_IO.Get( a_value ); Ada.Text_IO.Put( "Enter second value > " ); Ada.Integer_Text_IO.Get( b_value ); Ada.Text_IO.Put( "XOR value is " ); Ada.Integer_Text_IO.Put( a_value xor b_value ); Ada.Text_IO.New_Line; end DoIntXOR; BTW: Unless you're doing interface work, you rarely need to do bit fiddling in Ada. Steve (The Duck) "Peter C. Chapin" wrote in message news:MPG.1a516ba5b76b5bee989684@news.sover.net... > > Hello! > > I'm a C/C++ programmer in the process of teaching myself Ada. I've been > writing toy programs to get a feeling for the language. There is > obviously a lot to learn, but I'm enjoying myself. > > Anyway, I have a need to do some bit manipulations in one of my > programs. In particular I want to invert certain bits in a number > (integer). I'm used to doing this with a bitwise XOR operator (C/C++) so > I looked around for some kind of equivalent in Ada but I didn't find > anything in package Standard or in any of the "usual" library packages. > Am I missing something or am I just thinking about this wrong? > > Thanks! > > Peter