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
- TypeConverterHelper.cs
- ObjectQueryState.cs
- TextRangeEdit.cs
- ChtmlImageAdapter.cs
- ExpressionEditorAttribute.cs
- EditorPartChrome.cs
- Base64Encoder.cs
- TransformGroup.cs
- DiagnosticTraceSource.cs
- Frame.cs
- DeploymentExceptionMapper.cs
- Int32Converter.cs
- ProjectionAnalyzer.cs
- EdmItemError.cs
- SmiTypedGetterSetter.cs
- AsymmetricAlgorithm.cs
- WorkflowFormatterBehavior.cs
- FormViewInsertEventArgs.cs
- ObjectDataSourceSelectingEventArgs.cs
- CellTreeNodeVisitors.cs
- WebPartEditVerb.cs
- LookupBindingPropertiesAttribute.cs
- PeerCollaborationPermission.cs
- LoadItemsEventArgs.cs
- CollectionViewSource.cs
- FileSystemWatcher.cs
- DoubleAnimationUsingKeyFrames.cs
- LineProperties.cs
- ConnectionPointCookie.cs
- SelectionHighlightInfo.cs
- SocketAddress.cs
- FileDetails.cs
- EntityDataSourceDesigner.cs
- EventLogTraceListener.cs
- Rect3D.cs
- HtmlFormWrapper.cs
- RemotingServices.cs
- MetadataHelper.cs
- EmptyEnumerator.cs
- ScrollPattern.cs
- RectangleF.cs
- WebBaseEventKeyComparer.cs
- ToolStripDropDownItem.cs
- DNS.cs
- DynamicExpression.cs
- Attributes.cs
- WsdlContractConversionContext.cs
- ApplicationSettingsBase.cs
- AutomationElementCollection.cs
- URLMembershipCondition.cs
- SoapMessage.cs
- IgnoreSectionHandler.cs
- DataListItemCollection.cs
- PackageStore.cs
- SimpleWebHandlerParser.cs
- EventLogTraceListener.cs
- TreeNodeEventArgs.cs
- WindowsListViewItemCheckBox.cs
- GlobalizationSection.cs
- Int32RectValueSerializer.cs
- HandleCollector.cs
- SafeLibraryHandle.cs
- UnmanagedHandle.cs
- ProgressBarAutomationPeer.cs
- VectorValueSerializer.cs
- ModelPropertyDescriptor.cs
- UniqueIdentifierService.cs
- DropDownButton.cs
- ObjectItemAssemblyLoader.cs
- URLMembershipCondition.cs
- OpenTypeMethods.cs
- updateconfighost.cs
- OutputCacheSection.cs
- SQLCharsStorage.cs
- SqlCacheDependencyDatabaseCollection.cs
- DataProtectionSecurityStateEncoder.cs
- PictureBox.cs
- ValidatingCollection.cs
- ListenerSessionConnection.cs
- DetailsViewPagerRow.cs
- TraceUtils.cs
- FormViewAutoFormat.cs
- DialogDivider.cs
- DeviceContext2.cs
- UiaCoreProviderApi.cs
- CompareValidator.cs
- XPathMultyIterator.cs
- TextRunCache.cs
- ProtocolViolationException.cs
- MimeMultiPart.cs
- ToolStripSeparator.cs
- ColumnResult.cs
- UInt64.cs
- NetNamedPipeBindingElement.cs
- LinearGradientBrush.cs
- SqlAggregateChecker.cs
- RegionIterator.cs
- RegistrationServices.cs
- CanExecuteRoutedEventArgs.cs
- GlobalizationSection.cs