数据的存储(非完整版)

数据的存储

一.大小端

1.大小端简介

#include<stdio.h>

int main(void)

{

int a=0x11223344;//如图中内存高位11存储在高地址,读入时都由低地址读向高地址,所以转化为char类型为ox44。大端则相反高位低地址则为0x11;

printf("%x", *(char *)& a);//44

return 0;

}

 

2.判断大小端

(1).方法一(指针)

#include<stdio.h>

#include<stdbool.h>

int judge_model();

 int main(void)

{

 bool flag = judge_model();

 if (flag)

 printf("小端\n");

 else printf("大端\n");

}

 int judge_model()

{

 int a = 1;//00 00 00 01

 return *(char *)&a;//取a的地址转第0个字节的值进行返回

 //return (char*)&a;//把a的地址转换成char类型的地址

}

(2).方法二(联合体)

(2a).方法二

#include<stdio.h>

int jud();

int main(void)

{

if (jud())

printf("小端\n");

else printf("大端\n");

}

int jud()

{

union {

int a;

char ch;

}un;//联合体值相同,可以用来改变类型占用空间为字节最大的

un.a = 1;

return un.ch == 1;

}

(2b).联合体变量

#include<stdio.h>

int main(void)

{

union {

short a;

char ch[2];

}*s,b;

s = &b;

s->ch[0] = 0x39;//57,前两个字节赋值为57

s->ch[1] = 0x38;//56

printf("%x", b.a);//38 39

return 0;//变量由高地址到低地址

}

(3).方法3(定义新变量)

#include<stdio.h>

int main(void)

{

int a = 0x11223344;  //11 22 33 44

char res = (char)a;

}

1.

#include<stdio.h>

int main(void)

{

int x = 0;

void* a = &x;//void型的指针使其可以被任何指针赋值不用强制转化

}

二.补码

#include<stdio.h>

int main(void)

{

//数据永远使用补码储存

int a = 0x11223344;

//1000 0000 0000 0000 0000 0000 0000 1010 原

//1111 1111 1111 1111 1111 1111 1111 0101 反-符号位不变其他位取反

//1111 1111 1111 1111 1111 1111 1111 0110 补-反码+1

//F    F    F    F    F    F    F    6

int b = -10;

printf("%x", b);//fffffff6

}

1.截断和扩充

(1).unsigned

#include<stdio.h>

int main(void)

{

char a = -1;//1111 1111

//1000 0000 0000 0000 0000 0000 0000 0001

//1111 1111 1111 1111 1111 1111 1111 1110

//1111 1111 1111 1111 1111 1111 1111 1111

signed char b = -1;//同

unsigned char c = -1;//1111 1111

printf("%d %d %d", a, b, c);//a,b为复数返回%d返1,相同

//0000 0000 0000 0000 0000 0000 1111 1111 c 255

return 0;

}

(2).(unsigned int) -128

#include<stdio.h>

int main(void)

{

    //-128 ~ 127

    //1111 1111 1111 1111 1111 1111 1000 0000  补码

    char a = -128;  //1000 0000

    //1111 1111 1111 1111 1111 1111 1000 0000=4294967168

    printf("%u\n", a);

    return 0;

}

(3).+128 值 (越界)

#include<stdio.h>

int main(void)

{

char a = 128;

//0000 0000 0000 0000 0000 0000 0111 1111 127

//0000 0000 0000 0000 0000 000  1000 0000 128即为-127

printf("%u\n", a);

return 0;

}

(3a).越界死循环

#include<stdio.h>

int main(void)

{

unsigned int a = 9;//unsigned char a=0-255;

for (; a >= 0; --a)//a<256

{

printf("%u\n", a);

}

}

(3b).char 类型的变形(strlen 和\0)

#include<stdio.h>

int main(void)

{

int count = 0;

char a[1000];

for (int i = 0; i < 1000; i++)//循环1000次

{

a[i] = -1 - i; // -1 - -128 - 127 - 0

//a[i]=i;//打印的就是0

}

2.计算机加法模拟

#include<stdio.h>

void main()

{

int i = -20;

unsigned  int  j = 10;

//1000

//0000 0000 0000 0000 0000 0000 0000 1010  10

//1111 1111 1111 1111 1111 1111 1110 1100  -20

//1111 1111 1111 1111 1111 1111 1111 0110  补 -10

//1111 1111 1111 1111 1111 1111 1111 0101  反

//1111 1111 1111 1111 1111 1111 1111 1010  原

printf("%d\n", i + j);

}

printf("%d", strlen(a));//当遇到00 '\0'的ascll时会提前结束

return 0;

}

三.浮点型

1.浮点型的储存

#include<stdio.h>

int main(void)

{

int a = 4;

float b = 12.125;

//1100.001 1.100001 2^3

//S:符号

//M:尾码  1=<M<2

//E:阶码

//0 10000010 100 0010 0000 0000 0000 0000

//0100 0001 0100 0010 0000 0000 0000 0000

//4    1    4    2    0    0    0    0

//0.125*2=0.250*2=0.500*2=1.000

float c = 100.255;//0.255*2=0.510*2=1.02

//0110 0100.0100 0010

//1.100 1000 1000 0010

//0 5

//0 10000101 100 1000 1000 0010 ……

//0100 0010 1100 1000 1000 0010 ……

//4      2  c    8     8     2 ……

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值