[Oberon] Bit manipulation in Oberon-07

Tomas Kral thomas.kral at email.cz
Wed Oct 24 19:15:56 CEST 2018


On Wed, 24 Oct 2018 14:47:30 +0200
Tomas Kral <thomas.kral at email.cz> wrote:

> but how to access the
> individual bits?

How about this, if not correct or too silly?

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: INTEGER;
  b0,b1,b2,b3: BYTE; (*b0: LSB, b3: MSB*)
  bit0, bit1, bit2, bit3, bit4,  bit5, bit6, bit7: BYTE; (*bits' values*)
  s: SET;

  PROCEDURE Show*;
  BEGIN
    wd := 0F3F2F1F0H;
    b0 := wd MOD 100H; O.Hex(b0); O.Ln; (*one cycle or more?*)
    b1 := wd DIV 100H MOD 100H; O.Hex(b1); O.Ln;
    b2 := wd DIV 10000H MOD 100H; O.Hex(b2); O.Ln;
    b3 := wd DIV 1000000H MOD 100H; O.Hex(b3); O.Ln; O.Ln;
    bit0 := ORD(ODD(b0)); O.Hex(bit0); O.Ln;
    bit1 := b1 MOD 2; O.Hex(bit1); O.Ln;
    bit2 := b2 DIV 2 MOD 2; O.Hex(bit2); O.Ln;
    bit3 := b3 DIV 4 MOD 2; O.Hex(bit3); O.Ln;
    (*s := S.VAL(SET, wd) * m4);*) (*using mask {4}*)
    bit4 := b3 DIV 8 MOD 2; O.Hex(bit4); O.Ln;
    bit5 := b3 DIV 16 MOD 2; O.Hex(bit5); O.Ln;
    bit6 := b3 DIV 32 MOD 2; O.Hex(bit6); O.Ln;
    bit7 := b0 DIV 64 MOD 2; O.Hex(bit7); O.Ln; O.Ln;
    IF bit0 > 0 THEN INCL(s, bit0) END ;
    IF bit1 > 0 THEN INCL(s, bit1*2) END ;
    IF bit2 > 0 THEN INCL(s, bit2*4) END ;
    IF bit3 > 0 THEN INCL(s, bit3*8) END ;
    IF bit4 > 0 THEN INCL(s, bit4*16) END ;
    IF bit5 > 0 THEN INCL(s, bit5*32) END ;
    IF bit6 > 0 THEN INCL(s, bit6*64) END ;
    IF bit7 > 0 THEN INCL(s, bit7*128) END ;
    (*s := S.VAL(SET, wd) * (m0+m1+m2+m3);*) (*using mask {0..3}*)
    O.Hex(ORD(s)); O.Ln
  END Show;

BEGIN
END Bits.Show


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


More information about the Oberon mailing list