- Bit operations in Component Pascal

From: Andrew Thomas <"Andrew>
Date: Thu, 24 Mar 2005 14:21:44 -0000


Moi,

Yes SET is great and it is easy to go from SET to INTEGER. Things are much uglier in C. However C has an 4 byte unsigned integer and you can manipulate this as bits using <<, >>, ^ etc. This is very useful for writing random number generators where you can write
unsigned long y;

static int k = 0;

static unsigned long x[N]={ /* initial 25 seeds, change as you wish */

0x95f24dab, 0x0b685215, 0xe76ccae7, 0xaf3ec239, 0x715fad23,

0x24a590ad, 0x69e4b5ef, 0xbf456141, 0x96bc1b7b, 0xa7bdf825,

0xc1de75b7, 0x8858a9c9, 0x2da87693, 0xb657f9dd, 0xffdc8a9f,

0x8121da71, 0x8b823ecb, 0x885d05f5, 0x4e20cd47, 0x5a9ad5d9,

0x512c0c03, 0xea857ccd, 0x4cc1d30f, 0x8891a8a1, 0xa6b7aadb

};

x[kk] = x[kk+M] ^ (x[kk] >> 1) ^ mag01[x[kk] % 2];

and

y = x[k];

y ^= (y << 7) & 0x2b5b2500; /* s and b, magic vectors */

y ^= (y << 15) & 0xdb8b0000; /* t and c, magic vectors */

and

return( (double) y / (unsigned long) 0xffffffff);



The problem I have with the C code is x[kk] % 2 and the return statement. What is the best thing to do in CP?



Regards

    Andrew



----- Original Message -----
From: "Wojtek Skulski" <skulski{([at]})nowhere.xy
To: "BlackBox Mailing List" <blackbox{([at]})nowhere.xy
Sent: Monday, March 21, 2005 5:13 PM
Subject: [BlackBox] - Bit operations in Component Pascal


> Hi:
>
> one of the most useful features of Component Pascal,
> and perhaps least appreciated, are the bit manipulation
> intrinsics of the CP language. Today I showed the following
> program to one of my collegaues who was wondering how to
> manipulate bits. I am posting this program as a possible
> addition to the CP documentation being discussed on this list.
>
> It is wonderful to go back and forth between bitsets and integers
> and be able to manipulate bits directly without any obscurations.
>
> MODULE TstSetExample;
> IMPORT StdLog;
>
> PROCEDURE Do *;
> VAR s : SET;
> i : INTEGER;
> ans : BOOLEAN;
> BEGIN
> StdLog.String (" Example set operations ");
> StdLog.Ln;
> s := {0,1..3,15};
> StdLog.Set (s); StdLog.Ln;
> s := s + {7};
> StdLog.Set (s); StdLog.Ln;
> EXCL (s, 15);
> StdLog.Set (s); StdLog.Ln;
> s := BITS (22);
> i := ORD (s);
> StdLog.Int (i); StdLog.Ln;
> StdLog.Set (s); StdLog.Ln;
>
> FOR i := 0 TO MAX (SET) DO
> StdLog.Int (i); StdLog.String (" ");
> ans := i IN s;
> StdLog.Bool (ans);
> StdLog.Ln;
> END;
> END Do;
>
> END TstSetExample.
>
> <CTRL-Q> TstSetExample.Do
>
> --- BlackBox
> --- send subject HELP or UNSUBSCRIBE to blackbox{([at]})nowhere.xy
>
>





Received on Thu Mar 24 2005 - 15:21:44 UTC

This archive was generated by hypermail 2.3.0 : Thu Sep 26 2013 - 06:28:21 UTC