C++位操作包括两种:传统的C语言方式的位操作和C++中利用bitset容器的位操作
一、传统的C方式位操作:
1.基本操作:
使用一个unsigned int变量来作为位容器。
2.操作符:
| 按位或操作符:result=exp1|exp2;当exp1和exp2中对应位中至少有一个为1时,result中对应位为1,否则为0。
& 按位与操作符::result=exp1&exp2;当exp1和exp2中对应位全为1时,result中对应位为1,否则为0。
^ 按位异或或操作符:result=exp1^exp2;当exp1和exp2中对应位不相同时,result中对应位为1,否则为0。
~ 反转操作符:将位容器中的所有位都反转,1变为0,0变为1。
<< 按位左移操作符:exp<<n,将容器中所有的位向左移n位,空出的位用0填充。
>> 按位右移操作符:exp>>n,将容器中所有的位向右移n位,空出的位用0填充。
|=,&=,^= 分别对应|&^三种操作符的复合操作符。
3.常用操作
这里我们假设有一个result的unsigned int变量用来储存32个学生的成绩(通过和不通过分别用0和1),这样result就有33位(result从右至左,从0开始计算位数,在这 ......
在windows环境,我们有集成开发环境(IDE),使得我们对编译器了解的很少。当我们专向linux时需要在命令行下编译自己的程序需要对编译器的 命令行参数比较熟悉。而如果是做嵌入开发构建自己的操作系统时失去了系统平台,需要我们对编译的过程以及可执行文件的内部结构有所了解。本文讲述了如何编 译可执行文件、静态库、动态库,如何优化编译器,如何编译无操作系统环境下的程序(自己的OS)等。
1.分析普通的helloworld程序
先书写一下一个简单的helloworld程序如下:
/* hello.c */
int main(int argc, char * argv[])
{
return 0
}
编译程序:
gcc -o hello hello.c
等价的编译方法:
gcc -c hello.c
gcc -o hello.my -nostartfiles hello.o /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/crtn.o
我在redflag workstation 5.0版本下用3.4.3版本GCC编译器编译出来的大小都是3589字节,并且用diff命令比较为相同的文件。由此证明gcc在编译并链接 hello.c文件时先将hello.c编译成hello.o,然后将它与crt1.o、crti.o、crtn.o链接在一起。
&nbs ......
C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。
1. ldexp函数
ldexp函数的功能是计算value*2的幂,其用法为:double ldexp(double value, int exp);程序实例如下:
#include <stdio.h>
#include <math.h>
int main(void)
{
double value;
double x = 2;
value = ldexp(x,3);
printf("The ldexp value is: %lf\n",value);
return 0;
}
2. lfind函数
lfind函数的功能是执行线性搜索,其用法为:void *lfind(void *key, void *base, int *nelem, int width,int (*fcmp)()); 程序实例代码如下:
#include <stdio.h>
#include <stdlib.h>
int compare(int *x, int *y)
{
return( *x - *y );
}
int main(void)
{
int array[5] = {35, 87, 46, 99, 12} ......
C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。
1. ldexp函数
ldexp函数的功能是计算value*2的幂,其用法为:double ldexp(double value, int exp);程序实例如下:
#include <stdio.h>
#include <math.h>
int main(void)
{
double value;
double x = 2;
value = ldexp(x,3);
printf("The ldexp value is: %lf\n",value);
return 0;
}
2. lfind函数
lfind函数的功能是执行线性搜索,其用法为:void *lfind(void *key, void *base, int *nelem, int width,int (*fcmp)()); 程序实例代码如下:
#include <stdio.h>
#include <stdlib.h>
int compare(int *x, int *y)
{
return( *x - *y );
}
int main(void)
{
int array[5] = {35, 87, 46, 99, 12} ......
C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。
1. malloc函数
malloc函数的功能是内存分配函数,其用法为:void *malloc(unsigned size);程序实例如下:
#include <stdio.h>
#include <string.h>
#include <alloc.h>
#include <process.h>
int main(void)
{
char *str;
if ((str = malloc(10)) == NULL)
{
printf("Not enough memory to allocate buffer\n");
exit(1); /* terminate program if out of memory */
}
strcpy(str, "Hello");
printf("String is %s\n", str);
free(str);
return 0;
}
2. memchr函数
memchr函数的功能是在数组的前n个字节中搜索字符,其用法为:void *memchr(void *s, char ch, unsigned n);& ......
C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。
1. malloc函数
malloc函数的功能是内存分配函数,其用法为:void *malloc(unsigned size);程序实例如下:
#include <stdio.h>
#include <string.h>
#include <alloc.h>
#include <process.h>
int main(void)
{
char *str;
if ((str = malloc(10)) == NULL)
{
printf("Not enough memory to allocate buffer\n");
exit(1); /* terminate program if out of memory */
}
strcpy(str, "Hello");
printf("String is %s\n", str);
free(str);
return 0;
}
2. memchr函数
memchr函数的功能是在数组的前n个字节中搜索字符,其用法为:void *memchr(void *s, char ch, unsigned n);& ......
C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。
1. normvideo函数
malloc函数的功能是选择正常亮度字符,其用法为:void normvideo(void);程序实例如下:
#include <conio.h>
int main(void)
{
normvideo();
cprintf("NORMAL Intensity Text\r\n");
return 0;
}
2. open函数
open函数的功能是打开一个文件用于读或写,其用法为:int open(char *pathname, int access[, int permiss]); 程序实例代码如下:
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
int main(void)
{
int handle;
char msg[] = "Hello world";
if ((handle = open("TEST.$$$", O_CREAT | O_TEXT)) == -1)
{
perror("Error:");
return 1;
  ......
C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。
1. normvideo函数
malloc函数的功能是选择正常亮度字符,其用法为:void normvideo(void);程序实例如下:
#include <conio.h>
int main(void)
{
normvideo();
cprintf("NORMAL Intensity Text\r\n");
return 0;
}
2. open函数
open函数的功能是打开一个文件用于读或写,其用法为:int open(char *pathname, int access[, int permiss]); 程序实例代码如下:
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
int main(void)
{
int handle;
char msg[] = "Hello world";
if ((handle = open("TEST.$$$", O_CREAT | O_TEXT)) == -1)
{
perror("Error:");
return 1;
  ......
C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。
1. parsfnm函数
parsfnm函数的功能是分析文件名,其用法为:char *parsfnm (char *cmdline, struct fcb *fcbptr, int option);程序实例如下:
#include <process.h>
#include <string.h>
#include <stdio.h>
#include <dos.h>
int main(void)
{
char line[80];
struct fcb blk;
printf("Enter drive and file name (no path - ie. a:file.dat)\n");
gets(line);
if (parsfnm(line, &blk, 1) == NULL)
printf("Error in parsfm call\n");
else
printf("Drive #%d Name: %11s\n", blk.fcb_drive, blk.fcb_name);
return 0;
}
2. peek函数
peek函数的功能是检查存储单元,其用法为:int peek(int segment, uns ......
C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。
1. parsfnm函数
parsfnm函数的功能是分析文件名,其用法为:char *parsfnm (char *cmdline, struct fcb *fcbptr, int option);程序实例如下:
#include <process.h>
#include <string.h>
#include <stdio.h>
#include <dos.h>
int main(void)
{
char line[80];
struct fcb blk;
printf("Enter drive and file name (no path - ie. a:file.dat)\n");
gets(line);
if (parsfnm(line, &blk, 1) == NULL)
printf("Error in parsfm call\n");
else
printf("Drive #%d Name: %11s\n", blk.fcb_drive, blk.fcb_name);
return 0;
}
2. peek函数
peek函数的功能是检查存储单元,其用法为:int peek(int segment, uns ......