about summary refs log tree commit diff
path: root/lib/util/bitarith.h
blob: 4ed6c4c3694c60654dc6f6f04572a93d924fc39d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef BITARITH_H_INCLUDED
#define BITARITH_H_INCLUDED

#include "pm_config.h"

static __inline__ unsigned char
pm_byteLeftBits(unsigned char const x,
                unsigned int  const n) {
/*----------------------------------------------------------------------------
  Clear rightmost (8-n) bits, retain leftmost (=high) n bits.

  Return arbitrary value if n > 8.
-----------------------------------------------------------------------------*/
    unsigned char retval;

    retval = x;
    retval >>= (8-n);
    retval <<= (8-n);

    return retval;
}



static __inline__ unsigned char
pm_byteRightBits(unsigned char const x,
                 unsigned int  const n){
/*----------------------------------------------------------------------------
  Return rightmost (=low) n bits of x.

  Return arbitrary value if n > 8.
-----------------------------------------------------------------------------*/
    unsigned char retval;

    retval = x;
    retval <<= (8-n);
    retval >>= (8-n);

    return retval;
}

#endif