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-Thread: 103376,ddd2479bb7fae4af X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local1.nntp.dca.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for-mail NNTP-Posting-Date: Mon, 07 Feb 2005 21:09:14 -0600 From: "Steve" Newsgroups: comp.lang.ada References: <211db0ae.0502070437.54add641@posting.google.com> Subject: Re: Bit manipulation Date: Mon, 7 Feb 2005 19:11:08 -0800 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-RFC2646: Format=Flowed; Original Message-ID: NNTP-Posting-Host: 24.22.63.157 X-Trace: sv3-qHJoODzVBZ6fw6oCx6fbcWJqRAVzCpwbSAJw5H+WUvudEcHI9YftneBj9Ml1x8CH872tK6ywfpxI+ib!b9yZ5KoPr/k6LWwB63LEggQScIgus1+TJgPyAQ7S2McgmIR7msLL2YFAtCDK X-Complaints-To: abuse@comcast.net X-DMCA-Complaints-To: dmca@comcast.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.23 Xref: g2news1.google.com comp.lang.ada:8187 Date: 2005-02-07T19:11:08-08:00 List-Id: "Maurizio" wrote in message news:211db0ae.0502070437.54add641@posting.google.com... > hi, i need two hint: > > i need to acces to a 32 bit word (Interfaces.Unsigned_32). > how i can do to take the three m.s.bit? (30,31,32) > What do you mean by "take the three m.s.bit?" My guess is you either want to determine the value of the individual bits or you want to mask out the other bits. The following example should do either: input_value : Interfaces.Unsigned_32; masked_value : Interfaces.Unsigned_32; bit30 : Boolean; bit31 : Boolean; bit32 : Boolean; begin input_value := Get_Value( ... ); -- some function returning the value you want to mask masked_value = input_value and 16#E000_0000#; bit30 := (input_value and 16#2000_0000#) /= 0; bit31 := (input_value and 16#4000_0000#) /= 0; bit32 := (input_value and 16#8000_0000#) /= 0; Note: You could give the values in binary: 2#0010_0000_0000_0000_0000_0000_0000_0000# is the same as: 16#20000000# > second, i need to send the 32 bit word over a Tcp connection to a C > program, but > socket work with usigned 8 bit word, so i need to split the 32 bit in > 4 8bit word, > and when i read from the socket take 4 8bit word and combine in a 32 > word. You should look beware of "network byte order" when you do this mapping. The built in functions "htonl" and "ntohl" swizzle the bytes into network byte order (if they are available). Network byte order is basically big endian. I have seen a lot of code that ignores the standards for byte ordering, but the socket API's I have seen obey. Here's a snippet of code I use to swizzle the 32 bit floating point value into a 32 bit unsigned long value: TYPE aByte IS MOD 256; FOR aByte'SIZE USE 8; TYPE aByteArray IS ARRAY( Positive RANGE <> ) OF aByte'SIZE ; PRAGMA PACK( aByteArray ); FUNCTION hftonl( value : s_float ) RETURN u_long IS TYPE aFourBytes IS NEW aByteArray(1..4); FUNCTION Conv IS NEW Unchecked_Conversion( aFourBytes, u_long ); FUNCTION Conv IS NEW Unchecked_Conversion( s_float, aFourBytes ); temp : aFourBytes := Conv( value ); BEGIN RETURN Conv( aFourBytes'( temp(4), temp(3), temp(2), temp(1) ) ); END hftonl; I hope this helps, Steve (The Duck) > i see that Ada.Unchecked_Conversion work (in an ada client/server test > program) but is correct? > > --com_buffer_type is an array of 128 32bit word > > subtype Datas is Ada.Streams.Stream_Element_Array(1..512); --128*4 > > function To_Raw is > new Ada.Unchecked_Conversion > ( > Source => Com_Buffer_Type, --Interfaces.Unsigned_32 > Target => Datas); --Stream_Element is mod 2 ** > Standard'Storage_Unit > > function From_Raw is > new Ada.Unchecked_Conversion > ( > Source => Datas, > Target => Com_Buffer_Type); > > ---------------------- > best regards > Maurizio