c语言方向变量,C语言,变量与内存
一、數在計算機中的二進制表示
符號位:最高位為符號位,正數該位為0,負數該位為1;
原碼:原碼就是符號位加上真值的絕對值, 即用第一位表示符號, 其余位表示值
反碼:正數的反碼是其本身;負數的反碼是在其原碼的基礎上, 符號位不變,其余各個位取反。
補碼: 正數的補碼就是其本身;負數的補碼是在其原碼的基礎上, 符號位不變, 其余各位取反, 最后+1. (即在反碼的基礎上+1)。
正數的原碼、補碼、反碼表示方法均相同,不需轉換。
任何數值在內存中都是以補碼的形式存儲的。
正數的補碼與原碼相同。比如9的原碼和補碼都是1001。
負數的補碼等于它正數的原碼按位取反后再+1。
負數補碼計算:
1> -10的二進制形式 ? ? ? ?:1000 0000 0000 0000 0000 0000 0000 1010 //原
2> 除符號位取反 ? ? ? ? ? ? :1111 1111?1111 1111?1111 1111 1111 0101 //反
3> 對取反后的結果+1 ? ? ?:1111 1111?1111 1111?1111 1111 1111?0110 //補
二、基本數據類型
in linux-32:
#include "stdafx.h"
#include
#include
#include
#include "t.h"
void f0(void);
typedef struct ds{
unsigned int ui;
int i;
int j;
long int li;
char c;
unsigned char uc;
short int si;
unsigned short int usi;
float f;
double d;
long double ld;
char data[];
}DST;
int t1(void)
{
f0();
DST* pdst = (DST*)malloc(sizeof(DST)+1000);
if(pdst == 0){
return -1;
}
memset(pdst, 0, sizeof(DST)+1000);
pdst->ui = 0xffffffff;
pdst->i = pdst->ui;
pdst->j = -2147483648;
pdst->li = -2147483648;
pdst->c = -1;
pdst->uc = -1;
pdst->si = -1;
pdst->usi = -1;
pdst->f = -1;
pdst->d = -1;
pdst->ld = -1;
printf("pdst->c addr = %08x
", &(pdst->c) );
printf("pdst->uc addr = %08x
", &(pdst->uc) );
printf("pdst->si addr = %08x
", &(pdst->si) );
printf("pdst->usi addr = %08x
", &(pdst->usi) );
printf("pdst->f addr = %08x
", &(pdst->f) );
printf("pdst->d addr = %08x
", &(pdst->d) );
printf("pdst->ld addr = %08x
", &(pdst->ld) );
free(pdst);
pdst = 0;
return 0;
}
void f0(void)
{
printf("in vs2010:
");
printf("sizeof(char) = %d
", sizeof(char));
printf("sizeof(int) = %d
", sizeof(int));
printf("sizeof(short int) = %d
", sizeof(short int));
printf("sizeof(long int) = %d
", sizeof(long int));
printf("sizeof(float) = %d
", sizeof(float));
printf("sizeof(double) = %d
", sizeof(double));
printf("sizeof(long double) = %d
", sizeof(long double));
}
內存中的i是0xffffffff,
(1)轉化為二進制 ? 1111 1111?1111 1111?1111 1111 1111?1111
(2)發現是負數
(3)減一 ? ? ? ? ? ? ? 1111 1111?1111 1111?1111 1111 1111?1110
(4)除符號位取反 ? 1000 0000 0000 0000 0000 0000 0000 0001 ;?真值為-1
內存中的j是0x80000000,(最小的一個負值,暫時還不能理解,因為借了一位才能完成這換算,需33位,不知是不是因為cpu中的計算單元有33位以上寄存器。)
(1)轉化為2進制, 1000 0000 0000 0000 0000 0000 0000 0000
(2)發現是負數
(3)減一 ? ? ? ? ? ?1?0111 1111 1111 1111 1111 1111 1111 1111
(4)除符號位取反1 1000 0000 0000 0000 0000 0000 0000 0000 ;真值為-231= -2147483648
總結
以上是生活随笔為你收集整理的c语言方向变量,C语言,变量与内存的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux iptables 如何设置允
- 下一篇: linux小红帽实验心得,格林童话小红帽