Steffen Beyer > Bit-ShiftReg-2.0 > Bit::ShiftReg

Download:
Bit-ShiftReg-2.0.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 2.0   Source  

NAME ^

Bit::ShiftReg - Bit Shift Registers with Rotate / Shift Operations

Implements rotate left, rotate right, shift left and shift right operations with carry flag for all C integer types

SYNOPSIS ^

Note that "$value" must be a variable in the calls of the functions ROL, ROR, SHL and SHR, and that the contents of this variable are altered IMPLICITLY by these functions!

Note also that the "carry" input value is always truncated to the least significant bit, i.e., input values for "carry" must be either 0 or 1!

Finally, note that the return values of the functions LSB, MSB, ROL, ROR, SHL and SHR are always either 0 or 1!

DESCRIPTION ^

This module implements rotate left, rotate right, shift left and shift right operations with carry flag for all C integer types.

The results depend on the number of bits that the integer types unsigned char, unsigned short, unsigned int and unsigned long have on your machine.

The module automatically determines the number of bits of each integer type and adjusts its internal constants accordingly.

How the operations work:

ROL

Rotate Left:

 carry:                           value:

 +---+            +---+---+---+---     ---+---+---+---+
 | 1 |  <---+---  | 1 | 0 | 0 | 1  ...  1 | 0 | 1 | 1 |  <---+
 +---+      |     +---+---+---+---     ---+---+---+---+      |
            |                                                |
            +------------------------------------------------+
ROR

Rotate Right:

                        value:                          carry:

        +---+---+---+---     ---+---+---+---+           +---+
 +--->  | 1 | 0 | 0 | 1  ...  1 | 0 | 1 | 1 |  ---+---> | 1 |
 |      +---+---+---+---     ---+---+---+---+     |     +---+
 |                                                |
 +------------------------------------------------+
SHL

Shift Left:

 carry                        value:                       carry
  out:                                                      in:
 +---+        +---+---+---+---     ---+---+---+---+        +---+
 | 1 |  <---  | 1 | 0 | 0 | 1  ...  1 | 0 | 1 | 1 |  <---  | 1 |
 +---+        +---+---+---+---     ---+---+---+---+        +---+
SHR

Shift Right:

 carry                        value:                       carry
  in:                                                       out:
 +---+        +---+---+---+---     ---+---+---+---+        +---+
 | 1 |  --->  | 1 | 0 | 0 | 1  ...  1 | 0 | 1 | 1 |  --->  | 1 |
 +---+        +---+---+---+---     ---+---+---+---+        +---+

EXAMPLE ^

Suppose you want to implement shift registers in a machine-independent way.

The only C integer type whose length in bits you can be pretty sure about is probably a byte, since the C standard only prescribes minimum lengths for char, short, int and long and that sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long).

How to implement a 4-byte shift register and the 4 operations ROL, ROR, SHL and SHR on it:

First, you need to define 4 byte registers:

  $byte0 = 0;
  $byte1 = 0;
  $byte2 = 0;
  $byte3 = 0;

Then proceed as follows:

ROL

Rotate left:

  $carry = SHL_byte($byte3, SHL_byte($byte2, SHL_byte($byte1,
           SHL_byte($byte0, MSB_byte($byte3)))));
ROR

Rotate right:

  $carry = SHR_byte($byte0, SHR_byte($byte1, SHR_byte($byte2,
           SHR_byte($byte3, LSB_byte($byte0)))));
SHL

Shift left:

  $carry_out = SHL_byte($byte3, SHL_byte($byte2, SHL_byte($byte1,
               SHL_byte($byte0, $carry_in))));
SHR

Shift right:

  $carry_out = SHR_byte($byte0, SHR_byte($byte1, SHR_byte($byte2,
               SHR_byte($byte3, $carry_in))));

SEE ALSO ^

perl(1), perlsub(1), perlmod(1), perlxs(1), perlxstut(1), perlguts(1).

VERSION ^

This man page documents Bit::ShiftReg version 2.0.

AUTHOR ^

Steffen Beyer <sb@sdm.de>.

COPYRIGHT ^

Copyright (c) 1997 by Steffen Beyer. All rights reserved.

LICENSE AGREEMENT ^

This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: