Page 1 of 1

Convert INTEGER to SET

Posted: Mon Mar 21, 2011 1:34 pm
by Dimon
How do I convert a INTEGER to a SET?

Re: Convert INTEGER to SET

Posted: Mon Mar 21, 2011 9:35 pm
by cfbsoftware
If you want to typecast an INTEGER variable to a SET variable use the general typecasting function SYSTEM.VAL. e.g.

Code: Select all

VAR
  s: SET;
  i: INTEGER;

s := SYSTEM.VAL(SET, i);
Examples:

i = 0, s = {}
i = 8, s = {3}
i = 0FH, s = {0..3}
i = 0AAH, s = {1, 3, 5, 7}
i = 055H, s = {0, 2, 4, 6}

Note that this doesn't do any conversion of that sort that a function like Convert.IntToStr does, it just allows you to use SET operations on an INTEGER value as if it were a SET value. The bits in the word are not changed.

Re: Convert INTEGER to SET

Posted: Wed Sep 14, 2011 12:03 pm
by Eugene Temirgaleev
The note SET: A neglected data type, and its compilation for the ARM by Prof Wirth may be helpful for the understanding of type SET.

Re: Convert INTEGER to SET

Posted: Thu Sep 15, 2011 8:13 am
by Dimon
Eugene, I understand differense between numbers and bits.
My quest is technical and not theoretical. Context of this
quest is in constuction or definition of some NXP LPC
controllers control registers. Control register is presents
by a 32 bits processor word but structurally it is a packed
record with logic (BOOLEAN bit) and numeric (CARDINAL 2,
4 or generally N bit width) fields.
For example:

bit 0: boolean flag
bit 1: boolean flag

...
...
...

bit 11: boolean flag
bits from 12 to 15: The number of bytes, buffers or something else
bit 16: boolean flag

...

end of example.

I was need method to produce data of this kind.

I am sorry for my English. (On OberonCore I am Dmitry Maslov).

Re: Convert INTEGER to SET

Posted: Fri Sep 16, 2011 5:29 am
by Eugene Temirgaleev
Dimon wrote:Control register is presents
by a 32 bits processor word but structurally it is a packed
record with logic (BOOLEAN bit) and numeric (CARDINAL 2,
4 or generally N bit width) fields.
...
I was need method to produce data of this kind.

Code: Select all

word := ORD(setOfBooleanFlags) + LSL(cardinalField1, field1Pos) + ... + LSL(cardinalFieldN, fieldNPos)
word := ORD({1, 11, 16}) + LSL(15, 12)