Proposal for ISO C and C++
Bit Rotation Operators and Functions


Author: David R. Tribble
Date: 1998-03-07
Version: 1.00
Supersedes: None


Bit Rotation Operators and Functions

Introduction

C provides several "low-level" arithmetic operators that allow bit-level manipulation of integer (binary) quantities. However, one popular set of operations is missing from the language, and that is bit rotation operators.

This is a proposal for adding bit rotation operators to the language. It is composed of two major sections, Operators and Functions. (The intent is for only one of these sections to be accepted into the language.)


Operators

The following binary operators perform bitwise rotations:

    ^>		Right rotation
    ^<		Left rotation

    ^>=		Right rotation with assignment
    ^<=		Left rotation with assignment

Constraints

[#1] (for C only)
The left operand of a bit rotation operator shall be of an arithmetic integer or an enumeration type; all other types are invalid.

[#1] (for C++ only)
The left operand of a bit rotation operator shall be of an arithmetic integer type; all other types are invalid. In particular, the left operand shall not be of an enumeration type.

[#2]
The right operand of a bit rotation operator shall be of an arithmetic integer or an enumeration type. Whether or not operands are allowed with rank higher than (i.e., types wider than) int is implementation­defined.

[#3]
If the right operand is of a signed integer type and its value is negative, it is implementation­defined as to whether or not the result of the rotation expression is a meaningful arithmetic value.

[#4]
Whether or not any "bit holes" in the left operand are affected during the rotation operation is implementation­defined.

[#5]
Whether or not the bit pattern resulting from the rotatation operation causes an exception (i.e., a hardware trap) is implementation­defined.

[#6] (for C++ only)
The bit rotation operators may be defined (i.e., overloaded) as class member functions. There are no default or implicitly supplied rotation operators for a given class.

Examples

The following expressions illustrate the operation of the bit rotation operators:

    i = 0x0001 ^< 1;		// == 0x0002
    i = 0x0002 ^> 1;		// == 0x0001

    i = 0x0001 ^< 4;		// == 0x0010
    i = 0x0001 ^< 8;		// == 0x0100
    i = 0x0001 ^< 12;		// == 0x1000
    i = 0x0001 ^< 15;		// == 0x8000

    i = 0x8000 ^< 1;		// == 0x0001, for 16-bit int
    i = 0x0001 ^> 1;		// == 0x8000, for 16-bit int

    i = 0x0001 ^< 16;		// == 0x0001, for 16-bit int
    i = 0x0001 ^> 16;		// == 0x0001, for 16-bit int

    i = 0x0001 ^< 17;		// == 0x0002, for 16-bit int
    i = 0x0001 ^> 17;		// == 0x8000, for 16-bit int

    i = 0x3C5A ^< 1;		// == 0x78B4, for 16-bit int
    i = 0x3C5A ^< 2;		// == 0xF168, for 16-bit int
    i = 0x3C5A ^< 3;		// == 0xE2D1, for 16-bit int
    i = 0x3C5A ^< 4;		// == 0xC5A3, for 16-bit int
    i = 0x3C5A ^< 8;		// == 0x5A3C, for 16-bit int
    i = 0x3C5A ^< 12;		// == 0xA3C5, for 16-bit int
    i = 0x3C5A ^< 16;		// == 0x3C5A, for 16-bit int

    i = 0x3C5A ^> 1;		// == 0x1E2D, for 16-bit int
    i = 0x3C5A ^> 2;		// == 0x8F16, for 16-bit int
    i = 0x3C5A ^> 3;		// == 0x478B, for 16-bit int
    i = 0x3C5A ^> 4;		// == 0xA3C5, for 16-bit int
    i = 0x3C5A ^> 8;		// == 0x5A3C, for 16-bit int
    i = 0x3C5A ^> 12;		// == 0xC5A3, for 16-bit int
    i = 0x3C5A ^> 16;		// == 0x3C5A, for 16-bit int

    i ^<= 4;			// Rotate i left by 4 bits
    i ^>= 12;			// Rotate i right by 12 bits


Functions

The standard header <stdlib.h> contains declarations for the following library functions:

    std_rol()
    std_lrol()
    std_llrol()

    std_ror()
    std_lror()
    std_llror()

Function std_rol() et al

    unsigned int            std_rol(unsigned int x, int n);
    unsigned long int       std_lrol(unsigned long int x, int n);
    unsigned long long int  std_llrol(unsigned long long int x, int n);

The std_rol(), std_lrol(), and std_llrol() functions rotate the unsigned integer value x left by n bits, and return the resulting value.

If n is negative, the operation is equivalent to one of these:

    std_ror(x, -n)
    std_lror(x, -n)
    std_llror(x, -n)

If n is greater than the number of bits in x, the operation is equivalent to one of these:

    std_rol(x, n % MI)
    std_lrol(x, n % ML)
    std_llrol(x, n % MLL)
where MI, ML, and MLL are the number of bits in an unsigned int, unsigned long int, and unsigned long long int, respectively.

Function std_ror() et al

    unsigned int            std_ror(unsigned int x, int n);
    unsigned long int       std_lror(unsigned long int x, int n);
    unsigned long long int  std_llror(unsigned long long int x, int n);

The std_ror(), std_lror(), and std_llror() functions rotate the unsigned integer value x right by n bits, and return the resulting value.

If n is negative, the operation is equivalent to one of these:

    std_rol(x, -n)
    std_lrol(x, -n)
    std_llrol(x, -n)

If n is greater than the number of bits in x, the operation is equivalent to one of these:

    std_ror(x, n % MI)
    std_lror(x, n % ML)
    std_llror(x, n % MLL)
where MI, ML, and MLL are the number of bits in an unsigned int, unsigned long int, and unsigned long long int, respectively.


Discussion

If the addition of new operators is considered too drastic a change to the language, the addition of standard library functions should be sufficient in providing bit rotation capabilities to the programmer.

It is intended that either the operators or the functions, but not both, will be added to the language.


Prior Art

None.


References

None.


Comments? Send mail to David Tribble at work or home.
Link to David Tribble's home page.

Text Copyright ©1998 David R. Tribble, all rights reserved.