Re: Logical operations in BB

From: [at]} <Hans>
Date: Thu, 25 Jan 2001 01:45:19 +0100

Greg Edwards wrote:

>I'm trying to execute the following C++ line:
>nBytesPerLine := ( (nBytesPerLine + 31) & (~31) ) / 8;
>
>Is it true that BB doesn't have logical operators??? I've been searching
>everywhere and it seems to be you have to do something like:
> nBytesPerLine := SHORT(ENTIER(ORD(BITS(nBytesPerLine + 31) * BITS(31) ) /
>8));
>
>This seems awkward... any ideas how to better do this?

In Component Pascal (and in Modula and Oberon too) bitwise operations are
coded rather straitforwardly (imho) as SET operations.

Ofcourse, when coding bitwise operations you must know *exactly* what you
are doing. In the C++ line above the / operator functions as an integer
division operator, and so is translated into DIV, making the use of ENTIER
and SHORT unnecessary. Also you should not overlook the ~ operator (bitwise
complement), which is translated into - (SET complement).

So, assuming nBytesPerLine is a 32-bit integer variable, the Component
Pascal version would be (even without understanding what the C++ line tries
to accomplish):

  nBytesPerLine := ORD(BITS(nBytesPerLine + 31) * (-BITS(31))) DIV 8

When you realize that set difference is the same as intersection with the
complement (x - y = x * (-y)) then this could be shortened somewhat:

  nBytesPerLine := ORD(BITS(nBytesPerLine + 31) - BITS(31)) DIV 8

The set expression x - y simply means "exclude all bits (set them to 0)
from x that are member of y", which makes more sense to the non-brainwashed
than the C(++) expression x & ~y

Translation of other C(++) bit operators into Component Pascal set operators:

  & bitwise AND * set intersection
  | bitwise OR + set union
  ^ bitwise XOR / symmetric set difference
  ~ bitwise complement - set complement (unary operator)

Use of bitmasks in Component Pascal is easier than in C(++) because the bit
positions can directly be coded into the masking set. E.g. in your example
  x & ~31
is used to set the rightmost 5 bits of x to zero. In Component Pascal this
can be done by intersecting x with the set that lacks the bits 0..4:
  x * {5..MAX(SET)}

The C bitwise shift operators << and >> have a machine dependent effect in
case of signed integers, either arithmetic shift (with sign bits) or
logical shift (with zeros), translated into ASH(x, n) or SYSTEM.LSH(x, n)
in Component Pascal.


Hans Klaver


--------------------------------------------

To unsubscribe from this mailing list, send a message containing the word "unsubscribe" to:
   blackbox-request{([at]})nowhere.xy

To get a list of valid e-mail commands and instructions on their usage, send a message containing the word "help" to the above address.

Send any problem reports or questions related to this email list to the list owner at
   owner-blackbox{([at]})nowhere.xy
Received on Thu Jan 25 2001 - 00:45:19 UTC

This archive was generated by hypermail 2.3.0 : Thu Sep 26 2013 - 06:27:45 UTC