Board index » delphi » Bit Swapping

Bit Swapping

Hi,

I would like some help with a bit swapping routine that I have to write.
The code is to take a 5 bit input from a port then swap the bits.  

i.e. if the bit numbering is:   0 1 2 3 4
then it should end up as:     4 3 2 1 0

I have written a test program to do that but I am having problems with it.

The test program should:
write the individual values into an array so that:
9(dec) is written into an array of {0,1,0,0,1}
and bit swapped to become:
18(dec) {1,0,0,1,0}

and display on the screen these values.

I am using Borland Pascal 7.
There are two main problems that I am having.
1.  I can't seem to find a function that will raise a number to a power.  I
have written a messy function ( shown in the code below), but I was
wondering if there was either a built in function or a neater way of doing
this.
2.  The complier throws up a type mismatch error on line 51, following a
type cast then a maths operation on the casted value.

Thanks in advance,

Liam.

PROGRAM BitSwap;

USES crt, dos;

VAR
  BITSTORE : ARRAY [1..5] OF byte;
  TempByte : byte;
  PortA        : byte;
  Index         : integer;
  TempInt     : integer;
  IntPortA     : integer;

FUNCTION PowerOf( mantissa, exponent: integer) : integer;

VAR

RunningTotal : integer;
Index        : integer;

BEGIN

  RunningTotal := 1;

  IF exponent > 0 THEN
  BEGIN
    FOR Index := exponent DOWNTO 0 DO
    BEGIN
      RunningTotal := RunningTotal * mantissa;
    END;
  END
  ELSE
    RunningTotal := 1;

  PowerOf := RunningTotal;

END; (* of function PowerOf *)

(* Main Program *)

BEGIN

  PortA := 9;
  TempByte := 0;

  clrscr;
  writeln(PortA);

  FOR Index := 1 TO 5 DO
  BEGIN
    TempInt := PowerOf(2, (5-Index));
    writeln(TempInt);
    IntPortA := PortA SHL 5;
    BITSTORE[Index] := ((IntPortA)/(TempInt));
  END;

  FOR Index := 5 DOWNTO 1 DO
  BEGIN
    IF Index <> 5 then
      TempByte := (TempByte SHL 1) OR (BITSTORE[INDEX])
    ELSE
      TempByte := BITSTORE[INDEX];
  END;

  writeln(TempByte);

END.

--
-- All views expressed here are not those of my employer

 

Re:Bit Swapping


Liam Graves napisa3(a) w wiadomo?ci:
<01bf0bf1$b3069480$430e9482@basit17307643>...

Quote
>Hi,

>I would like some help with a bit swapping routine that I have to write.
>The code is to take a 5 bit input from a port then swap the bits.

>i.e. if the bit numbering is:   0 1 2 3 4
>then it should end up as:     4 3 2 1 0

If you really want to convert only 5 bits, the easiest,
fastest (and generally the best) way to do it is to declare
an array containing all the values you need and read
the result from the array. The array takes 32 bytes.
Less that any swapping function.

const rev:array[0..31] of byte = (0,16,8,24........);

So you have:
rev[0]=0
rev[1]=16 and rev[16]=1 etc.

Quote
>I have written a test program to do that but I am having problems with it.

>The test program should:
>write the individual values into an array so that:
>9(dec) is written into an array of {0,1,0,0,1}
>and bit swapped to become:
>18(dec) {1,0,0,1,0}

>and display on the screen these values.

Hmmm... You need a swapping function or you need a function
that exactly follows this algorithm?

Quote
>I am using Borland Pascal 7.
>There are two main problems that I am having.
>1.  I can't seem to find a function that will raise a number to a power.  I
>have written a messy function ( shown in the code below), but I was
>wondering if there was either a built in function or a neater way of doing
>this.

Try to use shl (shift left), equivalent to multiplying by
a power of 2:

  1 shl 1 = 2
  1 shl 2 = 4
  1 shl 3 = 8
  .....

Reverse: shift right, shr.

You might also want to use other functions:
  and
and
  or

The first can easy check if a bit is 0 or 1, the second
sets a bit to 1.

Piotr

Re:Bit Swapping


hi,

just do a shl or shr 8 times

Arash

Quote
Liam Graves wrote:
> Hi,

> I would like some help with a bit swapping routine that I have to write.
> The code is to take a 5 bit input from a port then swap the bits.

> i.e. if the bit numbering is:   0 1 2 3 4
> then it should end up as:     4 3 2 1 0

> I have written a test program to do that but I am having problems with it.

> The test program should:
> write the individual values into an array so that:
> 9(dec) is written into an array of {0,1,0,0,1}
> and bit swapped to become:
> 18(dec) {1,0,0,1,0}

> and display on the screen these values.

> I am using Borland Pascal 7.
> There are two main problems that I am having.
> 1.  I can't seem to find a function that will raise a number to a power.  I
> have written a messy function ( shown in the code below), but I was
> wondering if there was either a built in function or a neater way of doing
> this.
> 2.  The complier throws up a type mismatch error on line 51, following a
> type cast then a maths operation on the casted value.

> Thanks in advance,

> Liam.

> PROGRAM BitSwap;

> USES crt, dos;

> VAR
>   BITSTORE : ARRAY [1..5] OF byte;
>   TempByte : byte;
>   PortA        : byte;
>   Index         : integer;
>   TempInt     : integer;
>   IntPortA     : integer;

> FUNCTION PowerOf( mantissa, exponent: integer) : integer;

> VAR

> RunningTotal : integer;
> Index        : integer;

> BEGIN

>   RunningTotal := 1;

>   IF exponent > 0 THEN
>   BEGIN
>     FOR Index := exponent DOWNTO 0 DO
>     BEGIN
>       RunningTotal := RunningTotal * mantissa;
>     END;
>   END
>   ELSE
>     RunningTotal := 1;

>   PowerOf := RunningTotal;

> END; (* of function PowerOf *)

> (* Main Program *)

> BEGIN

>   PortA := 9;
>   TempByte := 0;

>   clrscr;
>   writeln(PortA);

>   FOR Index := 1 TO 5 DO
>   BEGIN
>     TempInt := PowerOf(2, (5-Index));
>     writeln(TempInt);
>     IntPortA := PortA SHL 5;
>     BITSTORE[Index] := ((IntPortA)/(TempInt));
>   END;

>   FOR Index := 5 DOWNTO 1 DO
>   BEGIN
>     IF Index <> 5 then
>       TempByte := (TempByte SHL 1) OR (BITSTORE[INDEX])
>     ELSE
>       TempByte := BITSTORE[INDEX];
>   END;

>   writeln(TempByte);

> END.

> --
> -- All views expressed here are not those of my employer

Re:Bit Swapping


Quote
Arash wrote:
> hi,

> just do a shl or shr 8 times

> Arash

Learn to quote correctly. This plus the binaries some time ago -> *plonk*

Re:Bit Swapping


Program Reverse;

Uses
        Crt;

Const
  InMask : Word = $0001;

  Mask4Bits : Word = $0008;
  Mask5Bits : Word = $0010;

Function ReverseBits ( theValue : Word; OutMask : Word ):Word;
Var
  newValue              : Word;
  SrcMask                       : Word;
Begin
  NewValue := 0;
  SrcMask := InMask;
  While OutMask <> 0 Do Begin
          If (theValue And SrcMask) <> 0 Then
      NewValue := NewValue Or OutMask;
    OutMask := OutMask Shr 1;
    SrcMask := SrcMask Shl 1;
  End;
  ReverseBits := newValue
End; { ReverseBits }

Procedure PrintBits ( theValue : Word; OutMask : Word );
Begin
  While OutMask <> 0 Do Begin
                If (theValue And OutMask) = 0 Then
      Write('0')
    Else
      Write('1');
      OutMask := OutMask Shr 1
  End
End; { PrintBits }

Var
  OutData               : Word;

begin
  OutData := $9;

  { Write the initial data }
  Write(OutData:6,'  '); { Note - No need for special conversion }
  PrintBits(OutData,Mask5Bits);
  WriteLn;
        { Reverse the bits }
        OutData := ReverseBits(OutData,Mask5Bits);

        { Now write the data out reversed }
        Write(OutData:6,'  '); { Note - No need for special conversion }
        PrintBits(OutData,Mask5Bits);
        WriteLn;

        Repeat Until KeyPressed
end.

Re:Bit Swapping


Quote
Liam Graves wrote:
> I would like some help with a bit swapping routine that I have to write.
> The code is to take a 5 bit input from a port then swap the bits.

> i.e. if the bit numbering is:   0 1 2 3 4
> then it should end up as:     4 3 2 1 0

You don't need an array to do that. The following function should
reverse a bit sequence, heeding only the 5 first bits of tue variable:

function Reverse(inp: byte): byte;
var
  i, b : byte;
begin
  b := 0;
  for i := 0 to 4 do
    if inp and (1 shl i) > 0 then b := b + (1 shl (4-i));
  Reverse := b
end;

Quote
> 1.  I can't seem to find a function that will raise a number to a power.  I
> have written a messy function ( shown in the code below), but I was
> wondering if there was either a built in function or a neater way of doing
> this.

function power(base, ex: longint): longint;
begin
  power := trunc(exp(ex * ln(base)))
end;

Quote
> 2.  The complier throws up a type mismatch error on line 51, following a
> type cast then a maths operation on the casted value.

Yes. You are assigning the result of a division to an item of an array
of bytes. Yet the result of such a division is always a real number and
can therefore not be assigned to a variable of type byte. Use the
function trunc to perform a type cast. See my power function above.

Re:Bit Swapping


Quote
Liam Graves wrote:
> Hi,

> I would like some help with a bit swapping routine that I have to write.
> The code is to take a 5 bit input from a port then swap the bits.

> i.e. if the bit numbering is:   0 1 2 3 4
> then it should end up as:     4 3 2 1 0

> I have written a test program to do that but I am having problems with it.

> The test program should:
> write the individual values into an array so that:
> 9(dec) is written into an array of {0,1,0,0,1}
> and bit swapped to become:
> 18(dec) {1,0,0,1,0}

> and display on the screen these values.

> I am using Borland Pascal 7.
> There are two main problems that I am having.
> 1.  I can't seem to find a function that will raise a number to a power.  I
> have written a messy function ( shown in the code below), but I was
> wondering if there was either a built in function or a neater way of doing
> this.

     You don't really NEED to "raise a number to a power".  Think about what
the bit operations really mean (hint -- it involves arithmetic operations
involving the number 2).
You may need to perform these operations repeatedly ...

Quote
> 2.  The complier throws up a type mismatch error on line 51, following a
> type cast then a maths operation on the casted value.

     Sorry, I didn't want to count the lines (are blank lines counted?) and
don't have a B
compiler to let it count for me.  Am I right in guessing the problem is with
the port i/o?
One thing you might consider is to read the data from the port into an integer
(or byte),
operate on that variable, then when done, write it back out to the port, rather
than trying to
involve the port in the arithmetic part of the operation.

Bob Schor
Pascal Enthusiast

Re:Bit Swapping


Arash wrote <37F4AFAF.4EA0F...@primus.com.au>:

Quote
>hi,

>just do a shl or shr 8 times

Well, the first will give you 256*initial_value, the second always 0...

PF

Re:Bit Swapping


Just a quick one to say thanks for all the help.  It was much appreciated
:?)

Liam.

Other Threads