From: Tom Backer Johnsen Subject: Re: Acessing bits in a 4-byte integer Date: 02 May 2000 00:00:00 GMT Message-ID: <390ECCD0.9E10DC1B@psych.uib.no> Content-Transfer-Encoding: 7bit References: <390F0A27.9C4408B9@psych.uib.no> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii Organization: University of Bergen, Norway Mime-Version: 1.0 Newsgroups: borland.public.delphi.objectpascal Its a good idea to start thinking before throwing out a question in general. The solution is very simple. Consider a type declaration like: type TIntegerset = set of 0..Sizeof(integer) * 8 - 1; with variables like: var Bits : TIntegerset; Value : integer; Then you may convert the integer to a set by by using: Bits := TIntegerset (Value); The set "Bits" will now consist of the numbers of the bits in "Value" which are set to 1, e.g. if bit 10 was set in Value, 10 will be in the "Bits" set. Working in the other direction: Value := Integer (Bits); Generates an integer value from the set. I think this will work. Very simple, and quite elegant. Tom Backer Johnsen wrote: > > I'd like to be able to be able to convert a 4-byte integer to an array > of integers, where each element contains the bit values (0 or 1) and the > corresponding operation in the opposite direction, i.e. from an integer > array with zero's and one's to a four byte integer. > > Is there a simple procedure to be used here in Delphi ? > > Tom -- +----------------------------------------------------------------+ | Tom Backer Johnsen, Psychometrics Unit, Faculty of Psychology | | University of Bergen, Christies gt. 12, N-5007 Bergen, NORWAY | | Tel : +47-5558-9185 Fax : +47-5558-9884 | | Email : backer@psych.uib.no URL : http://www.galton.uib.no/ | +----------------------------------------------------------------+