ATLAS Offline Software
Loading...
Searching...
No Matches
BitOp.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include <string>
6#include <algorithm>
7#include <sstream>
8
10
11/*******************************************************************
12 * $Date: 2009-05-27 18:18:06 +0200 (Wed, 27 May 2009) $
13 *
14 * Implementation of class BitOp
15 * @author Author: Thorsten Wengler
16 * @version $Revision: 187728 $
17 ******************************************************************/
18
19void BitOp::printBinN( unsigned int uintValue, int nbits ) {
20
21 std::string resultString( "" );
22 for ( int i = nbits; i >= 0; i-- ) {
23 if ( uintValue & ( 1u << i ) ) {
24 resultString += "1";
25 } else {
26 resultString += "0";
27 }
28 if ( ( i % 4 ) == 0 ) resultString += " ";
29 }
30
31}
32
33void BitOp::printBinN( int intValue, int nbits ) {
34
35 std::string resultString( "" );
36 for ( int i = nbits; i >= 0; i-- ) {
37 if ( intValue & ( 1u << i ) ) {
38 resultString += "1";
39 } else {
40 resultString += "0";
41 }
42 if ( ( i % 4 ) == 0 ) resultString += " ";
43 }
44
45}
46
47unsigned int BitOp::getValue( const unsigned int * uintValue,
48 const unsigned int mask ) {
49
50 unsigned int result;
51 unsigned int maskcopy;
52 // make a copy of the mask, because a mask is a mask and
53 // one should stay a mask (i.e. should be something constant!)
54 maskcopy = mask;
55 result = *uintValue & mask;
56 if ( mask != 0 ) {
57 while ( ( maskcopy & 0x00000001 ) == 0 ) {
58 maskcopy = maskcopy >> 1;
59 result = result >> 1;
60 }
61 }
62
63 return result;
64
65}
66
67unsigned int BitOp::alignBits( const int value, const int startbit, const int endbit )
68{
69 int ostart = std::min( startbit, endbit );
70 int oend = std::max( startbit, endbit );
71 unsigned int start = std::max( ostart, 0 );
72 unsigned int end = std::min( oend, 32 );
73
74 unsigned int result = value;
75 if ( ostart < 0 ) {
76 result >>= std::abs( ostart );
77 } else {
78 result <<= ostart;
79 }
80 result &= createMask( start, end );
81
82 return result;
83}
84
85unsigned int BitOp::createMask( const int startbit, const int endbit )
86{
87 unsigned int start = std::min( std::max( startbit, 0 ), std::max( endbit, 0 ) );
88 unsigned int end = std::max( std::min( startbit, 32 ), std::min( endbit, 32 ) );
89
90 unsigned int result = 0;
91 for( unsigned int i = 0; i <= ( end - start ); ++i ) {
92 result <<= 1;
93 result |= 1;
94 }
95 result <<= start;
96
97 return result;
98}
99
100const std::string BitOp::printBits( const int value, const int startbit, const int endbit )
101{
102 int ostart = std::min( startbit, endbit );
103 int oend = std::max( startbit, endbit );
104 unsigned int start = std::max( ostart, 0 );
105 unsigned int end = std::min( oend, 32 );
106
107 std::ostringstream s;
108
109 for( unsigned int i = start; i <= end; ++i ) {
110 if ( isSet(&value, i) ) {
111 s << "1";
112 } else {
113 s << "0";
114 }
115 }
116
117 return s.str();
118}
119
static unsigned int getValue(const unsigned int *uintValue, const unsigned int mask)
get the value in the input word represented by a bit pattern given as a bitmask
Definition BitOp.cxx:47
static unsigned int createMask(int startbit, int endbit)
create a 32 bit long mask with 1s from given start to end position
Definition BitOp.cxx:85
static unsigned int alignBits(int value, int startbit, int endbit)
align given bits using start and end position into 32 bits
Definition BitOp.cxx:67
static bool isSet(const unsigned int *uintValue, int bit)
Check if a the given bit in the given unsigned int or int value is set.
Definition BitOp.h:47
static void printBinN(unsigned int uintValue, int nbits)
Definition BitOp.cxx:19
static const std::string printBits(const int value, const int startbit, const int endbit)
print selected bit range into string
Definition BitOp.cxx:100