|
1 /* |
|
2 * Copyright (c) 2002-2007 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Bit manipulator. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "TSatBitOP.h" |
|
20 #include "SatLog.h" |
|
21 |
|
22 const TUint8 KBitsInByte( 8 ); |
|
23 const TUint8 KBitsInWord( 16 ); |
|
24 const TUint8 KHighBitInByte( 7 ); |
|
25 const TUint8 KHighBitInWord( 15 ); |
|
26 |
|
27 // ----------------------------------------------------------------------------- |
|
28 // Extracts bits from bit sequence. First bit is 0 index. |
|
29 // The lsb of extracted bits is set to 0 bit. |
|
30 // ----------------------------------------------------------------------------- |
|
31 // |
|
32 TUint8 TSatBitOP::ExtractBits( |
|
33 TInt aBitIndex, |
|
34 TUint8 aBitCount, |
|
35 const TDesC8& aBytes ) |
|
36 { |
|
37 LOG( DETAILED, "SATENGINE: TSatBitOP::ExtractBits calling" ) |
|
38 |
|
39 // Calculate the byte where aBitIndex is pointing. |
|
40 const TInt byteIndex( static_cast<TInt>( aBitIndex / KBitsInByte ) ); |
|
41 if ( byteIndex > 0 ) // aBitIndex is pointing other than first byte. |
|
42 { |
|
43 LOG( DETAILED, "SATENGINE: TSatBitOP::ExtractBits byteIndex > 0" ) |
|
44 // Change the aBitIndex to point the bits 0 to 7, because |
|
45 // we know which byte it is indexing. |
|
46 aBitIndex -= byteIndex * KBitsInByte; |
|
47 } |
|
48 |
|
49 TUint8 result( 0 ); |
|
50 // Check if the bits to be extracted is in two bytes. |
|
51 // ie. Part of the bits is in one byte and another |
|
52 // part is in the following byte. |
|
53 const TUint8 lastBit( static_cast<TUint8>( aBitIndex + aBitCount ) ); |
|
54 LOG2( DETAILED, "SATENGINE: TSatBitOP::ExtractBits lastBit: %i", lastBit ) |
|
55 if ( lastBit > KBitsInByte ) |
|
56 { |
|
57 // The bits to be extracted reside in two bytes. |
|
58 |
|
59 // Take the bytes. |
|
60 const TUint16 word( static_cast<TUint16>( |
|
61 ( aBytes[byteIndex] << KBitsInByte ) + aBytes[byteIndex + 1] ) ); |
|
62 |
|
63 // Create the mask, which extracts the bits. |
|
64 const TUint16 mask( |
|
65 CreateMask16Bit( static_cast<TUint8>( aBitIndex ), aBitCount ) ); |
|
66 |
|
67 // Extract the bits from the word. |
|
68 // Result will be shifted so that the lsb bit of the extracted bits |
|
69 // will be in bit 0. |
|
70 result = |
|
71 static_cast<TUint8>( ( word & mask ) >> ( KBitsInWord - lastBit ) ); |
|
72 } |
|
73 else |
|
74 { |
|
75 // Bits can be extracted from single byte. |
|
76 |
|
77 // Create the mask. |
|
78 const TUint8 mask( |
|
79 CreateMask8Bit( static_cast<TUint8>( aBitIndex ), aBitCount ) ); |
|
80 |
|
81 const TUint8 byte( aBytes[byteIndex] ); |
|
82 |
|
83 // Mask the bits and move the lsb of the extracted bits to 0 bit. |
|
84 // Result will be shifted so that the lsb bit of the extracted bits |
|
85 // will be in bit 0. |
|
86 result = |
|
87 static_cast<TUint8>( ( byte & mask ) >> ( KBitsInByte - lastBit ) ); |
|
88 } |
|
89 |
|
90 LOG( DETAILED, "SATENGINE: TSatBitOP::ExtractBits exiting" ) |
|
91 return result; |
|
92 } |
|
93 |
|
94 // ----------------------------------------------------------------------------- |
|
95 // Creates a 8-bit mask of ones. |
|
96 // ----------------------------------------------------------------------------- |
|
97 // |
|
98 TUint8 TSatBitOP::CreateMask8Bit( TUint8 aBitIndex, TUint8 aCount ) |
|
99 { |
|
100 LOG( DETAILED, "SATENGINE: TSatBitOP::CreateMask8Bit calling" ) |
|
101 |
|
102 // Change aBitIndex to use normal indexing of bits, because |
|
103 // 0 in aBitIndex means MSB. |
|
104 aBitIndex = static_cast<TUint8>( KHighBitInByte - aBitIndex ); |
|
105 |
|
106 TUint8 result( 0 ); |
|
107 TInt i ( 0 ); |
|
108 for ( i = 0; i < aCount; i++ ) |
|
109 { |
|
110 result |= static_cast<TUint8>( 1 << aBitIndex ); |
|
111 aBitIndex--; |
|
112 } |
|
113 LOG2( DETAILED, "SATENGINE: TSatBitOP::CreateMask8Bit i: %i", i ) |
|
114 LOG2( DETAILED, "SATENGINE: TSatBitOP::CreateMask8Bit exiting, \ |
|
115 result: %i", result ) |
|
116 return result; |
|
117 } |
|
118 |
|
119 // ----------------------------------------------------------------------------- |
|
120 // Creates a 16-bit mask of ones. |
|
121 // ----------------------------------------------------------------------------- |
|
122 // |
|
123 TUint16 TSatBitOP::CreateMask16Bit( TUint8 aBitIndex, TUint8 aCount ) |
|
124 { |
|
125 LOG( DETAILED, "SATENGINE: TSatBitOP::CreateMask16Bit calling" ) |
|
126 |
|
127 // Change aBitIndex to use normal indexing of bits, because |
|
128 // 0 in aBitIndex means MSB. |
|
129 aBitIndex = static_cast<TUint8>( KHighBitInWord - aBitIndex ); |
|
130 |
|
131 TUint16 result( 0 ); |
|
132 TInt i ( 0 ); |
|
133 for ( i = 0; i < aCount; i++ ) |
|
134 { |
|
135 result |= static_cast<TUint16>( 1 << aBitIndex ); |
|
136 aBitIndex--; |
|
137 } |
|
138 LOG2( DETAILED, "SATENGINE: TSatBitOP::CreateMask16Bit i: %i", i ) |
|
139 LOG2( DETAILED, "SATENGINE: TSatBitOP::CreateMask16Bit exiting, \ |
|
140 result: %i", result ) |
|
141 return result; |
|
142 } |