- 浏览: 401103 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
this is a thread that is inspired by the thoght and discusson from the stackoverflow. and hte original post is here: Bit fields in C#.
As a background/foundamental knowledge,or the first question to ask: what is bit fields?
Bit Fields is a extremely space saving technique which is quit commonly used in C++ proramms
here is one example of the BitField in C++:
class BitFields { public: int mode : 2; int full_flag : 1; int empty_flag : 1; };
and you can use it this way:
enum { On = 0, Off = 1 } ; enum { Yes = 0, No = 1 }; int main() { BitFields bitFields; bitFields.mode |= On; bitFields.full_flag &= ~Yes; }
Why do we need the Bit Fields classes, it is space effecient, and it is easy to manipulate. But they are not natively built inside the C# language.
Given an example.
suppose that we are going to model a flag as this:
byte-6 bit0 - original_or_copy bit1 - copyright bit2 - data_alignment_indicator bit3 - PES_priority bit4-bit5 - PES_scrambling control. bit6-bit7 - reserved
and you can do this in C
struct PESHeader { unsigned reserved:2; unsigned scrambling_control:2; unsigned priority:1; unsigned data_alignment_indicator:1; unsigned copyright:1; unsigned original_or_copy:1; };
Let's first examine some solution that has been discussed.
The StructLayoutAttribute solution
static class StructLayoutResolution { // the Fool structure is using a union approach. // or you can change the Explicit location so that they can aligh with each other sequentially // or you can try the LayoutKind.Sequential public void DemoStructLayoutResolution() { Foo foo = new Foo(); Console.WriteLine("foo.original_or_copy = {0}", foo.original_or_copy); } const byte _original_or_copy = 1; const byte _copyright = 2; static bool original_or_copy(this Foo foo) { return (foo.original_or_copy & _original_or_copy) == _original_or_copy; } } // you can use the StructlayoutAttribute // http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.structlayoutattribute%28VS.71%29.aspx [StructLayout(LayoutKind.Explicit, Size = 1, CharSet=CharSet.Ansi)] public struct Foo { [FieldOffset(0)] public byte original_or_copy; [FieldOffset(0)] public byte copyright; [FieldOffset(0)] public byte data_alignment_indicator; [FieldOffset(0)] public byte PES_prioirty; [FieldOffset(0)] public byte reserved; }
The code shown above is the union approach, but you can use the bit fields approach, by changing the Layoutkind from LayoutKind.Explicit to LayoutKind.Sequential.
The custom attributes solution
// this is using a custmo attribute , where you can decorate the fields just as you can do with the StructLayoutAttribute class CustomAttributeResoltuion { public static void DemoCustomAttributeTechnique() { PSEHeader p = new PSEHeader(); p.reserved = 3; p.scrambling_control = 2; p.data_alignment_indicator = 1; long l = PrimitiveConversion.ToLong(p); for (int i = 63; i >= 0; i--) { Console.WriteLine(((l & (1L << i)) > 0) ? "1" : "0"); } Console.WriteLine(); return; } } [AttributeUsage(AttributeTargets.Field, AllowMultiple=false)] sealed class BitfieldLengthAttribute : Attribute { uint length; public BitfieldLengthAttribute(uint length) { this.length = length; } public uint Length { get { return this.length; } } } // this is a helper class that helps you to convert a structure that is decorated with BitfieldLengthAttribute // to a long value static class PrimitiveConversion { public static long ToLong<T>(T t) where T : struct { long r = 0; int offset = 0; foreach (var f in t.GetType().GetFields()) { var attrs = f.GetCustomAttributes(typeof(BitfieldLengthAttribute), false); if (attrs.Length == 1) { uint fieldlength = ((BitfieldLengthAttribute)attrs[0]).Length; long mask = 0; for (int i = 0; i < fieldlength; i++) mask |= 1 << i; r |=( (UInt32)f.GetValue(t) & mask) << offset; offset += (int)fieldlength; } } return r; } } struct PSEHeader { [BitfieldLength(2)] public uint reserved; [BitfieldLength(2)] public uint scrambling_control; [BitfieldLength(1)] public uint priority; [BitfieldLength(1)] public uint data_alignment_indicator; [BitfieldLength(1)] public uint copyright; [BitfieldLength(1)] public uint original_or_copy; }
However, be cautions that this approach uses the Type.GetFields() call, which, according to the MSDN, does not guarantee to return the fields in the order that they are declared.
So you have to use it with caution.
BitVector approach
This is probably the most native one, the bit vector is a managed class and it is natural to use.
public static class BitVectorMethod { public static void BitVectorDemo() { rcSpan rcspan = new rcSpan(); rcspan.smin = 13; rcSpan2 rcspan2 = new rcSpan2(); rcspan2.smin = 13; } } public struct rcSpan { internal static readonly BitVector32.Section sminSection = BitVector32.CreateSection(0x1FFF); internal static readonly BitVector32.Section smaxSection = BitVector32.CreateSection(0x1FFF, sminSection); internal static readonly BitVector32.Section areaSection = BitVector32.CreateSection(0x3F, smaxSection); // the internal storage class object. internal BitVector32 data; // public uint smin: 13; public uint smin { get { return (uint)data[sminSection]; } set { data[sminSection] = (int)value; } } // public uint smax: 13; public uint smax { get { return (uint)data[smaxSection]; } set { data[smaxSection] = (int)value; } } // public uint area: 6; public uint area { get { return (uint)data[areaSection]; } set { data[areaSection] = (int)value; } } } // or you can do some handmade access this way public struct rcSpan2 { // andyou can do more // such as provide the handmade accessors for every fields internal uint data; // pulbic uint smin: 13 public uint smin { get { return (uint)data & 0x1FFFF; } set { data = (data & ~0x1FFFFu) | (value & 0x1FFFF); } } //public uint smax : 13; public uint smax { get { return (data >> 13) & 0x1FFF; } set { data = (data & ~(0x1FFFu << 13)) | (value & 0x1FFF) << 13; } } //public uint area : 6; public uint area { get { return (data >> 26) & 0x3F; } set { data = (data & ~(0x3F << 26)) | (value & 0x3F) << 26; } } }
As you can see, that in the code above, we are not only show the native bitvector approach, we are actually using some custom handmade bitvector acesss, but the priciple is the same.
发表评论
-
wpf - example to enhance ComboBox for AutoComplete
2014-09-19 15:56 1979first let’s see an example ... -
Investigate and troubleshoot possible memory leak issue of .NET application
2014-07-31 10:42 0Hi All, I would like to sh ... -
C# – CoerceValueCallback合并、替换元数据值
2013-08-05 21:59 1929Topic: C# – CoerceValueCallbac ... -
wpf – ListView交替背景色
2013-07-02 20:56 6555Wpf – Alternate background col ... -
C# - 简单介绍TaskScheduler
2013-06-29 17:18 12054标题: C# - 简单介绍TaskSchedulerTit ... -
c# - Get enum from enum attribute
2013-06-27 21:32 1252DescriptionAttribute gives the ... -
C# - PInvoke, gotchas on the RegisterClassEx and the CreateWindowEx
2013-06-24 13:49 2576I get an exception message li ... -
c# - Use PInvoke to create simple win32 Application
2013-06-24 11:59 10954In this post, .net platform h ... -
c# - Linq's Select method as the Map function
2013-06-19 18:47 1294If you comes from a functiona ... -
c# - Tips of Linq expression Any to determine if a collection is Empty
2013-06-19 18:29 942When you are @ the linq expres ... -
myth buster - typeof accepting array of types not acceptable
2013-06-19 17:17 819I have seen from some book whe ... -
windows - trying to create WIN32 application with PInvoke
2013-06-19 14:34 0While it is stupid to do such ... -
WPF - Setting foreground color of Entire window
2013-06-13 16:00 1925You might as well as I would s ... -
WPF - Enhanced TabControl - TabControlEx aka Prerendering TabControl
2013-06-13 13:12 5337As an opening word, let's che ... -
wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
2013-06-10 15:42 1191Recently I was trying to debug ... -
c# - P/Invoke, DllImport, Marshal Structures and Type conversions
2013-06-05 15:25 1717P/Invoke as in the following q ... -
c# - A study on the NativeWindow - encapsulate window handle and procedure
2013-06-05 14:40 6099NativeWindow gives you a way t ... -
WCF - Notify server when client connects
2013-06-03 18:19 1227It is sometimes very importan ... -
wcf - Debug to enable Server exception in Fault message
2013-06-03 15:47 1098WCF will be able to send back ... -
c# - determine if a type/object is serialzable
2013-05-30 16:35 870In WCF, primitives type are s ...
相关推荐
标题"C#-ymodem-update"指的是一个使用C#编程语言开发的YMODEM协议实现,主要功能是用于设备固件的升级,特别是针对bootloader的更新。YMODEM是一种流行的串行通信协议,用于在计算机之间传输文件,尤其是在低带宽和...
### C# 12 in a Nutshell:The Definitive Reference #### 1. Introducing C# and .NET **Object Orientation** C# is an object-oriented programming (OOP) language, which means that it structures code ...
C#-串口示例demo源代码-SerialPortHelper.rar,C#-串口示例demo源代码-SerialPortHelper.rar,C#-串口示例demo源代码-SerialPortHelper.rar
《大恒双相机开发-C#-多线程项目开源解析》 在当今信息化时代,高效、稳定的图像处理系统成为许多领域不可或缺的技术支持。本项目"大恒-双相机开发-C#-多线程"正是这样的一个实例,它利用C#语言进行编程,实现了对...
C# 。Net判断微信内置浏览器网页,微信内置浏览器打开的网页
### Exam 70-483 Programming in C# 关键知识点概述 #### 一、考试简介与背景 《Exam 70-483 Programming in C#》是微软官方认证的一项重要考试,主要针对那些希望证明自己具备使用C#语言进行高效编程能力的专业...
在本实例中,“protobuf实例-C#-聊天服务器”是基于C#语言实现的一个聊天服务项目,利用protobuf进行数据序列化和反序列化,以便于在网络通信中高效地传输聊天消息。 在C#环境下,protobuf提供了编译器工具,可以将...
C#-wxpay微信支付模板
1. **导入Material Design库**:首先,你需要在项目中引入Material Design In XAML Toolkit库,它为WPF提供了许多预定义的MD样式和控件。 2. **设置全局资源**:将库提供的ThemeDictionary添加到应用程序的资源字典...
名称: “C# - 模拟开心农场” 说明: C#模拟开心农场,经历播种、生长、开花、结果、收获的过程; 源代码分为两个版本: 一是手动实现成长收获的过程,另一版本是后台线程自动实现成长收获的过程 ^_^
实例32 <br>稿件名称:中英文手机短信PDU编码(UCS2)解码(UCS2,7-Bit) C# 程序 <br>稿件作者:李仓海 <br>程序名称:TC35iSMS <br>运行环境:TC35iSMS <br>注意事项:
intending to undertake the 70-483 Programming in C# certification exam in the near future. To make the path of learning C# easier for beginners, we have also tried to address the basics of C# and ...
在IT行业中,C#是一种广泛使用的编程语言,尤其在开发Web应用程序时,它与ASP.NET框架结合,可以创建高效、动态的网站。本教程将深入探讨如何使用C#和ASP.NET构建一个简易的聊天室网页。 首先,让我们理解标题"**C#...
【标题】"C#版USB-HID范例" 涉及的是在C#编程环境中与USB设备进行Human Interface Device (HID)通信的技术。USB-HID类库允许开发者直接与各种类型的输入设备(如键盘、鼠标、游戏控制器等)进行交互,而无需关心具体...
NovaMaker-2.4.29-win-64-bit.zip
Everything you need to make the move to C# programming is right here, in C# 2012 All-in-One For Dummies. From the Back Cover 7 books in 1 C# Programming Basics Object-Oriented C# Programming ...
32bit程序调用64bit dll 的解决办法 32bit程序不能直接调用64bit的dll,我们采用COM进程外组件的方式来实现间接调用。具体参考: http://blog.csdn.net/shakesky/article/details/23265811
C#网络电视源码 为了开源事业 支持动态生成播放列表
在C# WinForm开发中,数据展示和处理是常见的任务,而`dataGridView`控件是常用的界面元素,用于显示和编辑表格数据。本教程将详细讲解如何高效地将`dataGridView`中的数据批量导出到Excel文件,特别是针对大量数据...