首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > Multi Bit Mask
【标  题】:Multi Bit Mask
【关键字】:Multi,Bit,Mask
【来  源】:http://www.cppblog.com/shifan3/archive/2006/10/26/14264.html

Multi Bit Mask

C++博客 - shifan3 - Multi Bit Mask

shifan3

template is one doll!!

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  7 随笔 :: 0 文章 :: 14 评论 :: 0 Trackbacks

boost的integer/integer_mask.hpp仅仅做了单个位的bit mask
要多个位必须写很多遍high_bit_mask_t
使用low_bits_mask_t也不能完全解决问题
所以自己用Typelist的那种写法写了一个

用法举例
bit_mask<INT_LIST_2(2, 3)>::value返回一个值,该值的第2、3位被置为1
其余位为0



namespace ?multi_bit_mask
{
????
namespace ?details
????
{

????????
namespace ?storage_definition
????????
{
????????????template?
< int ?Bit >
????????????
struct ?bit_storage
????????????
{
????????????????typedef?typename?bit_storage
< Bit? - ? 1 > ::storage_type?storage_type;
????????????}
;

????????????
// ~~~~~~~~~~~~~~~platform?dependency,?modify?it?when?things?go?wrong~~~~~~~~~~~~~~~~~~~~~~~~~~

????????????typedef?unsigned?
int ?smallest_storage_type;

????????????template?
<>
????????????
struct ?bit_storage < 0 >
????????????
{
????????????????typedef?smallest_storage_type?storage_type;
????????????}
;

????????????template?
<>
????????????
struct ?bit_storage < 32 >
????????????
{
????????????????typedef?unsigned?
long ? long ?storage_type;
????????????}
;

????????????
// disable?the?65th?bit
????????????template? <>
????????????
struct ?bit_storage < 64 >
????????????
{
????????????????typedef?
void ?storage_type;
????????????}
;

????????????

????????????
// ~~~~~~~~~~~~~~~~~~~~~~~~~~end?of?platform?dependency~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
????????}



????????
namespace ?int_list_definition
????????
{
????????????template?
< unsigned? int ?N,?typename?Next >
????????????
struct ?int_list
????????????
{
????????????????typedef?typename?storage_definition::bit_storage
< N > ::storage_type?storage_type;
????????????????
static ? const ?storage_type?value? = ?N;
????????????????typedef?Next?next;
????????????}
;

????????????
struct ?null_type {} ;

????????????

????????????template
< typename?T1,?typename?T2,? bool ?is_first >
????????????
struct ?selector
????????????
{
????????????????typedef?T1?type;
????????????}
;

????????????template
< typename?T1,?typename?T2 >
????????????
struct ?compare_type
????????????
{
????????????????
const ? static ? bool ?is_larger? = ? sizeof (T1)? > ? sizeof (T2);
????????????????typedef?typename?selector
< T1,?T2,?is_larger > ::type?large_type;
????????????????typedef?typename?selector
< T1,?T2,? ! is_larger > ::type?small_type;
????????????}
;

????????????

????????????template
< typename?T1,?typename?T2 >
????????????
struct ?selector < T1,?T2,? false >
????????????
{
????????????????typedef?T2?type;
????????????}
;

????????????template?
< typename?List,? int ?N >
????????????
struct ?delete_from
????????????
{
????????????
private :
????????????????typedef?typename?delete_from
< typename?List::next,?N > ::result_type?deleted_next;
????????????
public :
????????????????typedef?int_list
< List::value,?deleted_next > ?result_type;
????????????}
;

????????????template?
< int ?N >
????????????
struct ?delete_from < null_type,?N >
????????????
{
????????????????typedef?null_type?result_type;
????????????}
;

????????????template?
< typename?Next,? int ?N >
????????????
struct ?delete_from < int_list < N,?Next > ,?N >
????????????
{
????????????????typedef?typename?delete_from
< Next,?N > ::result_type?result_type;
????????????}
;


????????????template?
< typename?List >
????????????
struct ?make_unique
????????????
{
????????????
private :
????????????????typedef?typename?make_unique
< typename?List::next > ::result_type?uniqued_next;
????????????????typedef?typename?delete_from
< uniqued_next,?List::value > ::result_type?prepared_next;
????????????
public :
????????????????typedef?int_list
< List::value,?prepared_next > ?result_type;
????????????}
;

????????????template?
<>
????????????
struct ?make_unique < null_type >
????????????
{
????????????????typedef?null_type?result_type;
????????????}
;
????????}


????????template?
< typename?List >
????????
class ?find_largest_storage
????????
{
????????????typedef?typename?find_largest_storage
< typename?List::next > ::storage_type?T1;
????????????typedef?typename?storage_definition::bit_storage
< List::value > ::storage_type?T2;
????????
public :
????????????typedef?typename?int_list_definition::compare_type
< T1,?T2 > ::large_type?storage_type;
????????}
;

????????template?
<>
????????
class ?find_largest_storage < int_list_definition::null_type >
????????
{
????????
public :
????????????typedef?storage_definition::smallest_storage_type?storage_type;
????????}
;
????}



????template?
< int ?N >
????
struct ?single_bit_mask
????
{
????????typedef?typename?details::storage_definition::bit_storage
< N > ::storage_type?storage_type;
????????
static ? const ?storage_type?value?
????????????
= ?static_cast < storage_type > (single_bit_mask < N? - ? 1 > ::value)? * ? 2 ;
????}
;

????template?
<>
????
struct ?single_bit_mask < 0 >
????
{
????????typedef?details::storage_definition::bit_storage
< 0 > ::storage_type?storage_type;
????????
static ? const ?storage_type?value? = ? 1 ;
????}
;

????


????template?
< typename?List >
????
struct ?bit_mask
????
{
????
private :
????????typedef?typename?details::int_list_definition::make_unique
< List > ::result_type?List_;
????
public :

????????typedef?typename?details::find_largest_storage
< List_ > ::storage_type?storage_type;
????
????????
static ? const ?storage_type?value?
????????????
= ?static_cast < storage_type > (single_bit_mask < List_::value > ::value)?
????????????
+ ?static_cast < storage_type > (bit_mask < typename?List_::next > ::value);
????}
;

????template?
<>
????
struct ?bit_mask < details::int_list_definition::null_type >
????
{
????????typedef?details::storage_definition::bit_storage
< 0 > ::storage_type?storage_type;
????????
static ? const ?storage_type?value? = ? 0 ;
????}
;


????typedef?multi_bit_mask::details::int_list_definition::null_type?null_type;

????template?
< int ?N,?typename?Next >
????
struct ?int_list_t?:? public ?multi_bit_mask::details::int_list_definition::int_list < N,?Next > ? {} ;

????
#define ?INT_LIST_1(n1)?multi_bit_mask::int_list_t<n1,?multi_bit_mask::null_type>
????
#define ?INT_LIST_2(n1,?n2)?multi_bit_mask::int_list_t<n1,?INT_LIST_1(n2)?>?
????
#define ?INT_LIST_3(n1,?n2,?n3)?multi_bit_mask::int_list_t<n1,?INT_LIST_2(n2,?n3)?>?
????
#define ?INT_LIST_4(n1,?n2,?n3,?n4)?multi_bit_mask::int_list_t<n1,?INT_LIST_3(n2,?n3,?n4)?>?
????
#define ?INT_LIST_5(n1,?n2,?n3,?n4,?n5)?multi_bit_mask::int_list_t<n1,?INT_LIST_4(n2,?n3,?n4,?n5)?>?
????
#define ?INT_LIST_6(n1,?n2,?n3,?n4,?n5,?n6)?multi_bit_mask::int_list_t<n1,?INT_LIST_5(n2,?n3,?n4,?n5,?n6)?>?
????
#define ?INT_LIST_7(n1,?n2,?n3,?n4,?n5,?n6,?n7)?multi_bit_mask::int_list_t<n1,?INT_LIST_6(n2,?n3,?n4,?n5,?n6,?n7)?>?
????
#define ?INT_LIST_8(n1,?n2,?n3,?n4,?n5,?n6,?n7,?n8)?multi_bit_mask::int_list_t<n1,?INT_LIST_7(n2,?n3,?n4,?n5,?n6,?n7,?n8)?>?
????
}



sample

#include? < iostream >
#include?
" multi_bit_mask.h "
using ? namespace ?std;
int ?main()
{
????cout?
<< ?multi_bit_mask::bit_mask < INT_LIST_1( 1 ) > ::value? << ?endl;
????cout?
<< ?multi_bit_mask::bit_mask < INT_LIST_5( 0 ,? 1 ,? 2 ,? 3 ,? 4 ) > ::value? << ?endl;
????cout?
<< ?multi_bit_mask::bit_mask < INT_LIST_7( 0 ,? 1 ,? 2 ,? 3 ,? 4 ,? 4 ,? 2 ) > ::value? << ?endl;
????
posted on 2006-10-26 23:37 Francis Arcanum 阅读(8) 评论(0)  编辑 收藏 收藏至365Key
PHP与CAS做SSO:【上一篇】
使用boost::thread::condition实现并发程序:【下一篇】
【相关文章】
  • bitao回答我的问题(请问.profile文件里的PATH问题)
  • Microsoft Windows Vista Build 5384.4 32Bit 简体中文版(转)
  • Java MultiThreading: wait()/notify()/notifyAll()
  • 64-bit CPU的朋友来看看
  • multiplexer protocol研究笔记
  • BitmapEffect学习
  • 打印每个数字的8个bit
  • debian的postfix Hosting Multiple Domains (即是虚拟域)
  • Cooperative Multithreading in BREW with IThread
  • DataTemplate Is A Bit Naughtier To Play With
  • 【随机文章】
  • Access键盘快捷键大全[下]
  • Maya 制作飘逸的秀发
  • 有趣的点
  • 自录DELPHI.NET 中ECO视频教程下载新地址
  • intel D945GNTL 主板在debian上的问题
  • 需要做几个FLASH动画和几十个网页
  • 用选择性粘贴实现图片转存
  • 通过IP Filter实现FreeBSD防火墙
  • 98和2K双系统并存常见问题解答
  • 搞到了visual studio 2005 prpfessional edtion
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.