《匠人手记》推荐网上购书渠道:
互动出版网(china-pub)购书入口   >>>
当当网(dangdang)购书入口   >>>
卓越亚马逊网 购书入口   >>>
淘宝网(taobao)购书入口   >>>
更多购书渠道……   >>> 

设为首页加入收藏联系匠人管理入口21IC首页21IC博客21IC社区侃单片机回复的贴参与的贴

天气预报
百宝日历
载入中...

百宝专栏

载入中...
最新货色

载入中...

粉丝评论

载入中...

载入中...



百宝信息

载入中...

百宝流量

(2006-07-01开始)


匠人手记

 匠人观点: 好记性不如烂笔头  
 黑色幽默:三鹿门——后世畅想

Steve's 'Cute Code' collection.
程序匠人 发表于 2005-9-9 8:20:00  阅读全文 | 回复(0) | 引用通告 | 编辑

Steve's 'Cute Code' collection.

Here is my collection of cute C and C++ tricks - I have tried to stick with code that is actually faster or more compact than the conventional way of doing things - or maybe the code just has to look pretty on the page - but this a personal collection, so I get to break the rules if I feel like it!

Unless I indicate otherwise, all variables are unsigned 32 bit integers.

Reverse all the bits in a 32 bit word:

I found this one in the Linux fortune cookie program (!)
   n = ((n >>  1) & 0x55555555) | ((n <<  1) & 0xaaaaaaaa) ;
   n = ((n >>  2) & 0x33333333) | ((n <<  2) & 0xcccccccc) ;
   n = ((n >>  4) & 0x0f0f0f0f) | ((n <<  4) & 0xf0f0f0f0) ;
   n = ((n >>  8) & 0x00ff00ff) | ((n <<  8) & 0xff00ff00) ;
   n = ((n >> 16) & 0x0000ffff) | ((n << 16) & 0xffff0000) ;

You can easily make versions of this for other word sizes.

Count the number of '1' bits in a 32 bit word:

John C. Wren <jcwren@jcwren.com> kindly sent me this one. It looks amazingly similar to the previous trick for bit reversal.
   n = (n & 0x55555555) + ((n & 0xaaaaaaaa) >> 1);
   n = (n & 0x33333333) + ((n & 0xcccccccc) >> 2);
   n = (n & 0x0f0f0f0f) + ((n & 0xf0f0f0f0) >> 4);
   n = (n & 0x00ff00ff) + ((n & 0xff00ff00) >> 8);
   n = (n & 0x0000ffff) + ((n & 0xffff0000) >> 16);

Test to see if a number is an exact power of two:

  b = ((n&(n-1))==0) ;

(NB: This code sets 'b' to TRUE if 'n' is an integer power of two - in this context, both zero and one are considered to be powers of two.)

This code actually works by changing the least significant '1' bit of 'n' to a '0'. If 'n' is a power of two, it only has one '1' bit - and zeroing it leaves you with zero as the answer.

Hence, the inner expression is also a cute trick...

Zero the least significant '1' bit in a word.

  n&(n-1)

Set the least significant N bits in a word.

  ~(~0<<n)

Swap the values of two integers.

This one is cute because it doesn't use a temporary variable and it's harder to get wrong than the usual code. It's been around for years, so I have no idea who invented it.
  x = x ^ y ;
  y = x ^ y ;
  x = x ^ y ;
 
[ The '^' operator stand for 'bitwise XOR' in C/C++ - not 'to the power of' as ex-FORTRAN people seem to think!]

However Scott Smith <ssmith@tyler.net> pointed out that this is equivelent to the even more aesthetically pleasing:

  x ^= y ^= x ^= y ;

It has been pointed out that this is strictly illegal C++ since the same variable is modified twice in one statement.

Convert a nibble into an ASCII hex digit.

Another ancient one. I think I first saw it in the sources for 'vi', but it's probably a lot older than that.
    "0123456789ABCDEF" [ n ]

(Where 'n' is in the range 0..15).

I have also seen this:

    n [ "0123456789ABCDEF" ]

...which also works providing 'n' is a character variable and providing you turn off enough compiler error checking!

Force all non-zero values to 1.

Pat Down sent me this one:
    b = !!a ;

b = 0 if a was 0 otherwise b = 1.

Little-Endian or Big-Endian?

Some computers store integers with the most significant data in the first byte and the least significant data in the last, others do it the other way around. The former type are called 'big-endian' and the latter 'little-endian'. Intel computers are traditionally little-endian and most others big-endian.

Most people do not realise that the terms 'big-endian' and 'little-endian' come from Gulliver's Travels. The nations of Lilliput and Blefuscu were waging a terrible and bloody war over which end one should cut open on a boiled egg - the little end or the big end.

The war between CPU manufacturers is just as silly - and also pretty damaging.

Gulliver says: "...all true Believers shall break their Eggs at the convenient End: and which is the convenient End, seems, in my humble Opinion, to be left to every Man's Conscience, or at least in the power of the Chief Magistrate to determine."

Hmmmm.

Anyway, the way to decide which kind of machine you have is:

  int i = 1 ;
  little_endian = *((char *) &i ) ;

Duffs Device

No collection would be complete without this:
 int a = some_number ;

 int n = ( a + 4 ) / 5 ;

 switch ( a % 5 )
 {
   case 0: do
           {
             putchar ( '*' ) ;
   case 4:   putchar ( '*' ) ;
   case 3:   putchar ( '*' ) ;
   case 2:   putchar ( '*' ) ;
   case 1:   putchar ( '*' ) ;
           } while ( --n ) ;
 }

 printf ( "\n" ) ;

The loop prints the 'a' asterisks - but is 'unrolled' (which is important for speed in some applications). Most people are suprised that this even compiles.

It has been said that the worst problem with Duff's device is knowing how to indent it!

看《匠人手记》,与匠人同行!北航出版,正在热卖!

发表评论:
载入中...

芯片专题

器件专题

软件专题

硬件专题

综合专题

项目专题

原创专题

器件检测
LCD LED
按键 触摸键
E2PROM
电池 电机
电阻 电容 电感

指令系统
软件算法
编程规范
滤波算法
串行通讯

PCB设计
I2C PWM
红外遥控
充电技术
中断 ADC 

匠人手记
匠人夜话
网络心路
一周热点串烧
从零开始玩PIC
DIY旋转时钟

广告5号位 [投放]


学习板、开发板、编程器、下载器、仿真器(查看详情……)

广告3号位 [投放]

站内搜索


站外搜索


百度  google
mp3  歌词 
图片  FLASH 
知道  文档
新闻  词典 
地图  mp3 
软件  天网 
雅虎  爱问 
搜狗  讯雷 
网讯  华军 
天空 

21IC器件搜索
百宝箱分站
  • 《匠人的百宝箱》21IC站
  • 《匠人的百宝箱》21IC笔记团队
  • 《匠人手记》21IC书友会
  • 《匠人的百宝箱》MCUBLOG站
  • 《匠人的百宝箱》MCUBLOG笔记团队
  • 《匠人的百宝箱》EDN站
  • 《匠人手记》EDN书友会
  • 《匠人的百宝箱》与非网站
  • 《匠人的百宝箱》新浪站
  • 《匠人的百宝箱》百度站
  • 《匠人的百宝箱》网易126站
  • 《匠人的百宝箱》网易163站
  • 《匠人的百宝箱》互动出版网站
  • 广告4号位 [投放]

     
     

    匠人原创

    往日酷贴

     
     
     

    大千八卦

    友情连接

    新浪新闻:
    新浪财经:
    AK58新闻:
    新浪股票:
    新浪股票:
    证券之星:

     [更多酷站连接]

     

     

    [欢迎交换连接]

    [百宝箱之与非门分舵]

    [电脑圈圈的家当]

    [IC921的博客]

    [柔月阁]

    [八楼的呼吸]

    [hotpower 的水潭]

    [xwj的文君阁]

    [所长的BLOG]

    [阿摆手记]

    [电子伙伴]

    [unaided的笔记]

    [小飞的笔记]

    [单片机开发联盟]

    [网址之家]

    [好东西网址大全]

    [美萍中文精选]

    [数字电视之家]

    [SMARTCODE电子书斋]

    [软件开发之窗]

    [Armoric]

    [我爱研发网]

    [infernal的笔记]

    [雄鹰的空中加油站]

    [SunK]

    [逍遥电子]

    [ningpanda的博客]

    [C-Design]

    [一网见天下]

    [海边淘沙]

    [嵌入式365]

    [水牛的仓库]

    [股剩是怎样炼成的]

    [PIC论坛]

    [ICC AVR开发网]

    [中国高校自动化网]

     

     

     

    MCU博客-中国电子工程师博客网 

    大学生电子网 

     

     

     

     

     

    !!! 《匠人的百宝箱》 !!!