[Oberon] Bit manipulation in Oberon-07

Tomas Kral thomas.kral at email.cz
Wed Oct 24 20:38:23 CEST 2018


On Wed, 24 Oct 2018 19:15:56 +0200
Tomas Kral <thomas.kral at email.cz> wrote:

> but how to access the
> individual bits?  

There is also SYSTEM.BIT(a, n) checking a single bit in memory.
Apologies, this is quite catchy and cannot let go yet. Rewritten in
arrays and loops, still possibly of little or no use.

  MODULE Bits; (*TK 24.10.2018 Word Byte Bit SET masks example*)
  IMPORT S := SYSTEM, O := Out;

CONST
  m0 = {0}; m1 = {1}; m2 = {2}; m3 = {3}; m4 = {4}; m5 = {5}; m6 = {6}; m7 = {7}; (*bit masks*)
  m0123 = {3,2,1,0}; (*lower nibble*) m4567 = {4,5,6,7}; (*upper nibble*)
  m16 = {0..15}; (*lower short*) m24 = {16..31}; (*upper short*)
  m31 = {0..31}; (*word*)

VAR
  wd, i: INTEGER;
  b: ARRAY 4 OF BYTE; (*b[0]: LSB, b[3]: MSB*)
  (*bit0, bit1, bit2, bit3, bit4,  bit5, bit6, bit7: BYTE;*) (*8 bits 0|1 values*)
  bit: ARRAY 8 OF BYTE;
  s: SET;

  PROCEDURE Show*;
  BEGIN
    wd := 0F3F2F1F0H; (*test word*)

    (*bytes)
    b[0] := wd MOD 100H; O.Hex(b[0]); O.Ln; (*shift = one cycle*)
    b[1] := wd DIV 100H MOD 100H; O.Hex(b[1]); O.Ln;
    b[2] := wd DIV 10000H MOD 100H; O.Hex(b[2]); O.Ln;
    b[3] := wd DIV 1000000H MOD 100H; O.Hex(b[3]); O.Ln; O.Ln;

    (*bits*)
    bit[0] := ORD(ODD(b[0])); O.Hex(bit[0]); O.Ln;
    bit[1] := b[1] MOD 2; O.Hex(bit[1]); O.Ln;
    bit[2] := b[2] DIV 2 MOD 2; O.Hex(bit[2]); O.Ln;
    bit[3] := b[3] DIV 4 MOD 2; O.Hex(bit[3]); O.Ln;

    (*s := S.VAL(SET, wd) * m4);*) (*using mask {4}*)

    bit[4] := b[3] DIV 8 MOD 2; O.Hex(bit[4]); O.Ln;
    bit[5] := b[3] DIV 16 MOD 2; O.Hex(bit[5]); O.Ln;
    bit[6] := b[3] DIV 32 MOD 2; O.Hex(bit[6]); O.Ln;
    bit[7] := b[0] DIV 64 MOD 2; O.Hex(bit[7]); O.Ln; O.Ln;

    (*set unrolled*)
    (*IF bit[0] > 0 THEN INCL(s, 0) END ;
    IF bit[1] > 0 THEN INCL(s, 1) END ;
    IF bit[2] > 0 THEN INCL(s, 2) END ;
    IF bit[3] > 0 THEN INCL(s, 3) END ;
    IF bit[4] > 0 THEN INCL(s, 4) END ;
    IF bit[5] > 0 THEN INCL(s, 5) END ;
    IF bit[6] > 0 THEN INCL(s, 6) END ;
    IF bit[7] > 0 THEN INCL(s, 7) END ;*)

    (*set in loop*)
    s := {}; FOR i := 0 TO 7 DO IF bit[i] > 0 THEN INCL(s, i) END END ;
    (*s := S.VAL(SET, wd) * (m0+m1+m2+m3);*) (*using mask {0..3}*)

    (*output byte hex & bin*)
    O.Hex(S.VAL(INTEGER, s)); O.Ln;
    O.Bin(S.VAL(INTEGER, s), 8); O.Ln
  END Show;

BEGIN
END Bits.Show


-- 
Tomas Kral <thomas.kral at email.cz>


More information about the Oberon mailing list