易截截图软件、单文件、免安装、纯绿色、仅160KB

我的C实践(9):位和字节的重排

  位和字节的重排在密码学算法中有广泛的应用。
/* rearran.c:位和字节的重排 */
/* 位反转:以字的中心为对称点进行位反射
例如: abcd efgh ijkl mnop ABCD EFGH IJKL MNOP
位反转:PONM LKJI HGFE DCBA ponm lkji hgfe dcba */
unsigned rev(unsigned x){
/* 交换相邻的单个位 */
x=(x & 0x55555555)<<1 | (x & 0xaaaaaaaa)>>1;
/* 交换相邻的2位字段 */
x=(x & 0x33333333)<<2 | (x & 0xcccccccc)>>2;
/* 交换相邻的4位字段 */
x=(x & 0x0f0f0f0f)<<4 | (x & 0xf0f0f0f0)>>4;
/* 交换相邻的8位字段 */
x=(x & 0x00ff00ff)<<8 | (x & 0xff00ff00)>>8;
/* 交换相邻的16位字段 */
x=(x & 0x0000ffff)<<16 | (x & 0xffff0000)>>16;
return x;
}
/* 字节反转:以字的中心为对称点进行字节反射
例如: abcd efgh ijkl mnop ABCD EFGH IJKL MNOP
字节反转:IJKL MNOP ABCD EFGH ijkl mnop abcd efgh */
unsigned revw(unsigned x){
x=(x & 0x00ff00ff)<<8 | (x & 0xff00ff00)>>8;
x=(x & 0x0000ffff)<<16 | (x & 0xffff0000)>>16;
return x;
}
/* 位混洗:将右半字的各个位相间地插入到左半字中,尾部的位仍然保留在尾部
例如:abcd efgh ijkl mnop ABCD EFGH IJKL MNOP
混洗:aAbB cCdD eEfF gGhH iIjJ kKlL mMnN oOpP */
unsigned shuffling(unsigned x){
unsigned t;
/* 初始:abcd efgh ijkl mnop ABCD EFGH IJKL MNOP */
/* abcd efgh ABCD EFGH ijkl mnop IJKL MNOP */
t=(x ^ (x>>8)) & 0x0000ff00; x=x ^ t ^ (t<<8);
/* abcd ABCD efgh EFGH ijkl IJKL mnop MNOP */
t=(x ^ (x>>4)) & 0x00f000f0; x=x ^ t ^ (t<<4);
/* abAB cdCD efEf ghGH ijIJ klKL mnMN opOP */
t=(x ^ (x>>2)) & 0x0c0c0c0c; x=x ^ t ^ (t<<2);
/* aAbB cCdD eEfF gGhH iIjJ kKlL mMnN oOpP */
t=(x ^ (x>>1)) & 0x22222222; x=x ^ t ^ (t<<1);
return x;
}
/* 逆混洗 */
unsigned unshuffling(unsigned x){
/* 以相反的顺序进行交换即可实现逆混洗 */
t=(x ^ (x>>1)) & 0x22222222; x=x ^ t ^ (t<&


相关文档:

通过#pragma pack(n)改变C编译器的字节对齐方式

在C语言中,结构是一种复合数据类型,其构成元素既可以是基本数据类型(如int、long、float等)的变量,也可
以是一些复合数据类型(如数组、结构、联合等)的数据单元。在结构中,编译器为结构的每个成员按其自然对界(alignment)条件分配空间。各个成员
按照它们被声明的顺序在内存中顺序存储,第一个成员的地址和整个 ......

linux内核移植s3c2410,移植正式开始2

内核启动的现在已经是开始执行函数start_kernel函数了。start_kernel函数在init/main.c中定义。start_kernel函数只是完成
相应的结构的初始化任务。
    printk(KERN_NOTICE);
    printk(linux_banner);
    setup_arch(&command_line);
在uboot的一直过程中,uboo ......

C 标准库 函数 源代码的实现 和 分析

//库函数实现
char *strcat (char *dst, const char *src)
{
  char *p = dst;
  while (*p)
    p++;
  while ((*p++ = *src++))
    ;
  return dst;
}
char *strncat (char *s1, const char *s2, long unsigned int n)
{
  char *dest = s1;
  ......

关于c,c++输出格式控制

1.
printf("%.9lf\n",sum); 
//输出小数点后9位不省去末尾多余的0
2.
printf("%.10g\n",sum);
 //输出小数点后9位不省去末尾多余的0
3.
#include<iomanip>
cout<<setprecision(10)<<sum<<endl;
//输出小数点后9位省去末尾多余的0
4. 
#include<iomanip>
cout.pre ......

c程序的预处理,编译,连接过程。

自己搜集并且整理,用于参考。
1.完整流程:
C源程序头文件-->预编译处理(cpp)-->编译程序本身-->优化程序-->汇编程序-->链接程序-->可执行文件
 
 
 
pic from http://edmulroy.portbridge.com/oview.htm
 
2. c的预处理
预编译程序所完成的基本上是对源程序的& ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号