|
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 class SymByte |
|
24 { |
|
25 #region Constructors |
|
26 public SymByte( byte aValue ) |
|
27 { |
|
28 iValue = aValue; |
|
29 } |
|
30 |
|
31 public SymByte( string aBinary ) |
|
32 { |
|
33 iValue = (byte) SymBitUtils.CreateMask( aBinary ); |
|
34 } |
|
35 #endregion |
|
36 |
|
37 #region API |
|
38 public bool IsMatch( string aBinary ) |
|
39 { |
|
40 SymByte value = 0; |
|
41 SymByte mask = SymBitUtils.CreateMask( aBinary, out value ); |
|
42 bool ret = ( mask & iValue ) == value; |
|
43 return ret; |
|
44 } |
|
45 |
|
46 public SymByte LowestBits( int aCount ) |
|
47 { |
|
48 // Make the mask |
|
49 byte mask = 0; |
|
50 for ( int i = 0; i < aCount; i++ ) |
|
51 { |
|
52 mask |= (byte) ( 0x1 << i ); |
|
53 } |
|
54 |
|
55 // Apply it |
|
56 byte ret = (byte) ( iValue & mask ); |
|
57 return new SymByte( ret ); |
|
58 } |
|
59 |
|
60 public SymByte HighestBitsShiftedRight( int aCount ) |
|
61 { |
|
62 // Make the mask |
|
63 byte mask = 0; |
|
64 for ( int i = 7; i >=0; i-- ) |
|
65 { |
|
66 mask |= (byte) ( 0x1 << i ); |
|
67 } |
|
68 |
|
69 // Apply it |
|
70 byte ret = (byte) ( iValue & mask ); |
|
71 |
|
72 // Shift |
|
73 ret >>= aCount; |
|
74 |
|
75 return new SymByte( ret ); |
|
76 } |
|
77 |
|
78 public void RShift( int aBits ) |
|
79 { |
|
80 iValue >>= aBits; |
|
81 } |
|
82 |
|
83 public uint LShift( int aBits ) |
|
84 { |
|
85 uint ret = (uint) ( iValue << aBits ); |
|
86 return ret; |
|
87 } |
|
88 #endregion |
|
89 |
|
90 #region Properties |
|
91 public byte Value |
|
92 { |
|
93 get { return iValue; } |
|
94 set { iValue = value; } |
|
95 } |
|
96 |
|
97 public string Binary |
|
98 { |
|
99 get |
|
100 { |
|
101 StringBuilder ret = new StringBuilder(); |
|
102 ret.Append( Convert.ToString( Value, 2 ).PadLeft( 8, '0' ) ); |
|
103 return ret.ToString(); |
|
104 } |
|
105 } |
|
106 |
|
107 public bool this[ int aIndex ] |
|
108 { |
|
109 get |
|
110 { |
|
111 byte mask = (byte) ( 0x1 << aIndex ); |
|
112 bool set = ( mask & Value ) == mask; |
|
113 return set; |
|
114 } |
|
115 } |
|
116 #endregion |
|
117 |
|
118 #region Operators |
|
119 public static implicit operator byte( SymByte aByte ) |
|
120 { |
|
121 return aByte.Value; |
|
122 } |
|
123 |
|
124 public static implicit operator SymByte( byte aByte ) |
|
125 { |
|
126 return new SymByte( aByte ); |
|
127 } |
|
128 |
|
129 public static bool operator ==( SymByte aLeft, SymByte aRight ) |
|
130 { |
|
131 bool result = aLeft.Value == aRight.Value; |
|
132 return result; |
|
133 } |
|
134 |
|
135 public static bool operator !=( SymByte aLeft, SymByte aRight ) |
|
136 { |
|
137 bool result = !( aLeft == aRight ); |
|
138 return result; |
|
139 } |
|
140 |
|
141 public static SymByte operator &( SymByte aLeft, SymByte aRight ) |
|
142 { |
|
143 byte result = (byte) ( aLeft.Value & aRight.Value ); |
|
144 return new SymByte( result ); |
|
145 } |
|
146 #endregion |
|
147 |
|
148 #region From System.Object |
|
149 public override string ToString() |
|
150 { |
|
151 StringBuilder ret = new StringBuilder(); |
|
152 ret.AppendFormat( "{0:x2} [{1}]", iValue, System.Convert.ToString( iValue, 2 ).PadLeft( 8, '0' ) ); |
|
153 return ret.ToString(); |
|
154 } |
|
155 |
|
156 public override bool Equals( object aObject ) |
|
157 { |
|
158 bool ret = false; |
|
159 // |
|
160 if ( aObject != null && aObject is SymByte ) |
|
161 { |
|
162 SymByte other = (SymByte) aObject; |
|
163 ret = ( other.Value == this.Value ); |
|
164 } |
|
165 // |
|
166 return ret; |
|
167 } |
|
168 |
|
169 public override int GetHashCode() |
|
170 { |
|
171 return iValue.GetHashCode(); |
|
172 } |
|
173 #endregion |
|
174 |
|
175 #region Internal methods |
|
176 #endregion |
|
177 |
|
178 #region Data members |
|
179 private byte iValue = 0; |
|
180 #endregion |
|
181 } |
|
182 } |