位,字符,C字符串


#位,字符,字符串

位运算符

基本概念

  1. 大多数系统中,8位构成一个自己-—字符型变量标准存储单位。其他类型数据以更多字节存储。位运算符用来操作整型操作数(char,int,short,long,包括signed和unsigned), 中的位。通常处理unsigned类型整数。
  2. 位运算符:位与(&),位或(|),位异或(^),位取反(~),左移(<<)——右侧用0填充,右移(>>)——左侧空位填充与机器相关.
  3. 原码,反码,补码。计算机存储的是补码
    正数:原码=反码=补码
    负数:反码=原码除符号位外按位取反,补码=反码+1

代码演示

#include <iostream>
#include <iomanip>
using namespace std;

void displayBits(unsigned);
int main()
{
    unsigned value = 0;
    cout << "Input a value: \n";
    cin >> value;
    displayBits(value);

    return 0;
}
void displayBits(unsigned value)
{
    const int SHIFT = 8 * sizeof(unsigned) - 1;
    const unsigned MASK = 1 << SHIFT;

    cout << setw(10) << value << " = ";
    for (unsigned i = 1; i <= SHIFT + 1;i++)
    {
        cout << (value & MASK ? '1' : '0');  //关键部分,按位输出字符
        value <<= 1;

        if(i%8==0)
            cout << ' ';
    }
    cout << endl;
}

位域

C++ 允许设定类或结构体的 int 类型或枚举类型成员的存储位数,这样的成员称为位域。位域成员必须声明为整型或枚举类型。

    //位域
    struct BitCard
    {
        unsigned face : 13;       //声明face为 4位宽度的位域,能表示0~15
        unsigned      : 3;       //allign to next storage-unit boundary
        unsigned suit : 2;
        unsigned      : 0;       //跳过suit所在存储单元剩余位,从下一个存储单元开始存储color
        unsigned color : 1;
    };

可以指定一个未命名位域,用来在结构体中进行填充.如使用3位未命名位域进行填充,不存储任何内容。成员suit就被存储在另一个存储单元。
尽管可以节省空,位域会导致编译器产生低效的机器语言代码,需要额外的机器语言访问存储单元的一部分。

字符处理库

isdigit
isalpha
isalnum
isxdigit
toupper
isspace
iscntrl
ispunct
iaprint
isgraph

C字符串操作函数

strcpy
strncpy
strcat
strncat
strcmp
strncmp
strtok
strlen

C字符串转换函数

atof
atoi
atol
strtod
strtol
strtoul

C字符串操作库中的搜索函数

strchr
strcspn
strpbrk
strrchr
strspn
strstr

C字符串操作库中的内存函数

memcpy
memmove
memcmp
memchr
memset

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值