|
1 /* |
|
2 * Copyright (c) 2009 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: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 using System.Collections.Generic; |
|
19 using System.Text; |
|
20 |
|
21 namespace SymbianUtils.BasicTypes |
|
22 { |
|
23 public static class SymBitUtils |
|
24 { |
|
25 #region API |
|
26 public static uint CreateMask( string aBinary ) |
|
27 { |
|
28 uint value; |
|
29 return CreateMask( aBinary, out value ); |
|
30 } |
|
31 |
|
32 public static uint CreateMask( string aBinary, out uint aExpectedValue ) |
|
33 { |
|
34 uint mask = 0; |
|
35 aExpectedValue = 0; |
|
36 |
|
37 int bit = 0; |
|
38 int count = aBinary.Length; |
|
39 for ( int charIndex = count - 1; charIndex >= 0; charIndex-- ) |
|
40 { |
|
41 // Get a character from the string |
|
42 char c = Char.ToLower( aBinary[ charIndex ] ); |
|
43 // |
|
44 if ( c == SymBitUtils.KBitIsSet ) |
|
45 { |
|
46 mask |= (uint) ( 1 << bit ); |
|
47 aExpectedValue |= (uint) ( 1 << bit ); |
|
48 } |
|
49 else if ( c == SymBitUtils.KBitIsClear ) |
|
50 { |
|
51 mask |= (uint) ( 1 << bit ); |
|
52 } |
|
53 else if ( c == SymBitUtils.KBitIsNotApplicable1 || c == SymBitUtils.KBitIsNotApplicable2 ) |
|
54 { |
|
55 } |
|
56 // |
|
57 if ( c != SymBitUtils.KBitIsReadabilitySpacer ) |
|
58 { |
|
59 ++bit; |
|
60 } |
|
61 } |
|
62 // |
|
63 return mask; |
|
64 } |
|
65 |
|
66 public static byte CreateMask( string aBinary, out byte aExpectedValue ) |
|
67 { |
|
68 uint value; |
|
69 uint ret = CreateMask( aBinary, out value ); |
|
70 // |
|
71 if ( ret > 0xFF || value > 0xFF ) |
|
72 { |
|
73 throw new ArgumentException( "Binary sequence is too large to fit byte" ); |
|
74 } |
|
75 // |
|
76 aExpectedValue = (byte) value; |
|
77 return (byte) ret; |
|
78 } |
|
79 |
|
80 public static SymByte CreateMask( string aBinary, out SymByte aExpectedValue ) |
|
81 { |
|
82 byte value = 0; |
|
83 SymByte ret = CreateMask( aBinary, out value ); |
|
84 aExpectedValue = value; |
|
85 return ret; |
|
86 } |
|
87 |
|
88 public static string GetBits( byte aByte ) |
|
89 { |
|
90 string ret = System.Convert.ToString( aByte, 2 ).PadLeft( 8, KBitIsClear ); |
|
91 return ret; |
|
92 } |
|
93 |
|
94 public static uint StringToUint( string aText ) |
|
95 { |
|
96 uint ret = 0; |
|
97 // |
|
98 int bit = 0; |
|
99 int count = aText.Length; |
|
100 for ( int charIndex = count - 1; charIndex >= 0; charIndex--, ++bit ) |
|
101 { |
|
102 char c = char.ToLower( aText[ charIndex ] ); |
|
103 // |
|
104 if ( c == KBitIsSet ) |
|
105 { |
|
106 uint mergeIn = (uint) ( 1 << bit ); |
|
107 ret |= mergeIn; |
|
108 } |
|
109 } |
|
110 // |
|
111 return ret; |
|
112 } |
|
113 |
|
114 public static string BeautifyBits( uint aAddress, int aNumberOfKnownBits ) |
|
115 { |
|
116 string binAddress = System.Convert.ToString( aAddress, 2 ); |
|
117 // |
|
118 binAddress = binAddress.PadLeft( 32, '0' ); |
|
119 binAddress = binAddress.Substring( 32 - aNumberOfKnownBits, aNumberOfKnownBits ); |
|
120 binAddress = binAddress.PadLeft( 32, 'x' ); |
|
121 binAddress = BeautifyBits( binAddress ); |
|
122 // |
|
123 return binAddress; |
|
124 } |
|
125 |
|
126 public static string BeautifyBits( string aBits ) |
|
127 { |
|
128 StringBuilder ret = new StringBuilder( aBits.PadLeft( 32, 'x' ) ); |
|
129 ret.Insert( 24, ' ' ); |
|
130 ret.Insert( 16, ' ' ); |
|
131 ret.Insert( 08, ' ' ); |
|
132 return ret.ToString(); |
|
133 } |
|
134 |
|
135 public static uint RotateRight( uint aValue, int aCount ) |
|
136 { |
|
137 uint ret = aValue; |
|
138 // |
|
139 for ( int i = 0; i < aCount; i++ ) |
|
140 { |
|
141 bool bitZeroSet = ( ( ret & 0x1 ) == 0x1 ); |
|
142 ret >>= 1; |
|
143 // |
|
144 if ( bitZeroSet ) |
|
145 { |
|
146 // Wrap the bit around by appling it at the top end |
|
147 ret |= KTopBit; |
|
148 } |
|
149 } |
|
150 // |
|
151 return ret; |
|
152 } |
|
153 |
|
154 public static SymBit GetBit( uint aValue, int aBitNumber ) |
|
155 { |
|
156 uint mask = 1u << aBitNumber; |
|
157 uint value = aValue & mask; |
|
158 SymBit ret = ( value != 0 ) ? SymBit.ESet : SymBit.EClear; |
|
159 return ret; |
|
160 } |
|
161 #endregion |
|
162 |
|
163 #region Internal constants |
|
164 internal const char KBitIsSet = '1'; |
|
165 internal const char KBitIsClear = '0'; |
|
166 internal const char KBitIsNotApplicable1 = '#'; |
|
167 internal const char KBitIsNotApplicable2 = 'x'; |
|
168 internal const char KBitIsReadabilitySpacer = ' '; |
|
169 private const uint KTopBit = 0x80000000; |
|
170 #endregion |
|
171 } |
|
172 } |