Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WCF / Serialization / System / Runtime / Serialization / BitFlagsGenerator.cs / 1305376 / BitFlagsGenerator.cs
//------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace System.Runtime.Serialization { using System; using System.Reflection; using System.Reflection.Emit; using System.Security; [Fx.Tag.SecurityNote(Miscellaneous = "RequiresReview (Critical) - works on CodeGenerator objects, which require Critical access.")] class BitFlagsGenerator { int bitCount; CodeGenerator ilg; LocalBuilder[] locals; public BitFlagsGenerator(int bitCount, CodeGenerator ilg, string localName) { this.ilg = ilg; this.bitCount = bitCount; int localCount = (bitCount + 7) / 8; locals = new LocalBuilder[localCount]; for (int i = 0; i < locals.Length; i++) { locals[i] = ilg.DeclareLocal(typeof(byte), localName + i, (byte) 0); } } public static bool IsBitSet(byte[] bytes, int bitIndex) { int byteIndex = GetByteIndex(bitIndex); byte bitValue = GetBitValue(bitIndex); return (bytes[byteIndex] & bitValue) == bitValue; } public static void SetBit(byte[] bytes, int bitIndex) { int byteIndex = GetByteIndex(bitIndex); byte bitValue = GetBitValue(bitIndex); bytes[byteIndex] |= bitValue; } public int GetBitCount() { return bitCount; } public LocalBuilder GetLocal(int i) { return locals[i]; } public int GetLocalCount() { return locals.Length; } public void Load(int bitIndex) { LocalBuilder local = locals[GetByteIndex(bitIndex)]; byte bitValue = GetBitValue(bitIndex); ilg.Load(local); ilg.Load(bitValue); ilg.And(); ilg.Load(bitValue); ilg.Ceq(); } public void LoadArray() { LocalBuilder localArray = ilg.DeclareLocal(Globals.TypeOfByteArray, "localArray"); ilg.NewArray(typeof(byte), locals.Length); ilg.Store(localArray); for (int i = 0; i < locals.Length; i++) { ilg.StoreArrayElement(localArray, i, locals[i]); } ilg.Load(localArray); } public void Store(int bitIndex, bool value) { LocalBuilder local = locals[GetByteIndex(bitIndex)]; byte bitValue = GetBitValue(bitIndex); if (value) { ilg.Load(local); ilg.Load(bitValue); ilg.Or(); ilg.Stloc(local); } else { ilg.Load(local); ilg.Load(bitValue); ilg.Not(); ilg.And(); ilg.Stloc(local); } } static byte GetBitValue(int bitIndex) { return (byte)(1 << (bitIndex & 7)); } static int GetByteIndex(int bitIndex) { return bitIndex >> 3; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- IResourceProvider.cs
- SrgsDocument.cs
- KeyValueConfigurationCollection.cs
- HttpResponseWrapper.cs
- RuleSettings.cs
- ScriptResourceInfo.cs
- SamlNameIdentifierClaimResource.cs
- DataListItemEventArgs.cs
- LinqDataSourceDeleteEventArgs.cs
- ButtonField.cs
- DownloadProgressEventArgs.cs
- Equal.cs
- NamespaceMapping.cs
- ParentControlDesigner.cs
- Vector3DAnimationUsingKeyFrames.cs
- Point3DValueSerializer.cs
- InstallerTypeAttribute.cs
- Canvas.cs
- DataBindEngine.cs
- SpStreamWrapper.cs
- WebPartsPersonalizationAuthorization.cs
- DiscoveryClientProtocol.cs
- ListViewAutomationPeer.cs
- HttpListenerResponse.cs
- UInt16Storage.cs
- DiagnosticsElement.cs
- DataStreams.cs
- PrintDocument.cs
- DesigntimeLicenseContextSerializer.cs
- FaultCode.cs
- DbUpdateCommandTree.cs
- RequiredFieldValidator.cs
- HitTestWithGeometryDrawingContextWalker.cs
- DynamicResourceExtension.cs
- HyperLinkDataBindingHandler.cs
- XamlDebuggerXmlReader.cs
- ListItemCollection.cs
- Tokenizer.cs
- CollectionChangedEventManager.cs
- ExceptionHelpers.cs
- XomlCompilerError.cs
- Error.cs
- FormsAuthenticationUserCollection.cs
- DataExchangeServiceBinder.cs
- HtmlInputButton.cs
- ControlDesigner.cs
- ScriptResourceInfo.cs
- VBIdentifierNameEditor.cs
- NativeMethods.cs
- StopRoutingHandler.cs
- CodeTypeReferenceExpression.cs
- PeerObject.cs
- RadioButton.cs
- Setter.cs
- M3DUtil.cs
- SafeCryptoHandles.cs
- XmlSchemaSimpleTypeRestriction.cs
- CssClassPropertyAttribute.cs
- TextEffectResolver.cs
- SendMailErrorEventArgs.cs
- TailPinnedEventArgs.cs
- RuntimeIdentifierPropertyAttribute.cs
- SigningProgress.cs
- ThicknessAnimationBase.cs
- CodeGen.cs
- DataGridViewMethods.cs
- TextBoxAutomationPeer.cs
- PreviewPrintController.cs
- InfoCardRSAPKCS1SignatureDeformatter.cs
- ErrorFormatter.cs
- DbQueryCommandTree.cs
- SplitterPanel.cs
- LinkedList.cs
- Common.cs
- UrlPropertyAttribute.cs
- XsdDateTime.cs
- _RequestCacheProtocol.cs
- MetaTableHelper.cs
- DesignerAttribute.cs
- WebRequest.cs
- XmlSigningNodeWriter.cs
- HtmlAnchor.cs
- MtomMessageEncodingBindingElement.cs
- DataGridViewRowStateChangedEventArgs.cs
- ParameterDataSourceExpression.cs
- GifBitmapDecoder.cs
- RoutedEventArgs.cs
- Query.cs
- UnsafeNativeMethods.cs
- DataGridViewComboBoxEditingControl.cs
- ListViewDeleteEventArgs.cs
- HttpModuleCollection.cs
- SqlParameter.cs
- SplineQuaternionKeyFrame.cs
- Selection.cs
- CodeExpressionStatement.cs
- HttpResponseMessageProperty.cs
- ControlValuePropertyAttribute.cs
- SafeEventHandle.cs
- NavigationPropertyAccessor.cs