int n=5;
我們提供的服務(wù)有:成都網(wǎng)站制作、成都網(wǎng)站設(shè)計、外貿(mào)營銷網(wǎng)站建設(shè)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、雨城ssl等。為上1000+企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學管理、有技術(shù)的雨城網(wǎng)站制作公司
double x=3.141592654;
while(n0) //取出小數(shù)后n位
{
x=x*10; //更新x值,把小數(shù)點后第一位放在個位上
printf("%d\n",(int)x%10); //int強制把double轉(zhuǎn)換成int,因為求余必須為int類型, //而%10求余,是求個位上的數(shù)字
i--;
}
bit是二進制的位的意思 如果你要操作位 最好用位運算符 等
128的二進制表示:?10000000
從右往左數(shù)第八位是1
答案也簡單:
C語言版:
int?getbit(int?n,?int?k){
return?(n(k-1))??1;
}
我有點沒搞明白(45、81、673938)取最后一位是怎么得出5138的,不是518嗎?
但還是隨便寫一下吧
#include?stdio.h
#include?stdlib.h
int?getLastBit(int?number);
int?main()
{
int?i?=?0,?n?=?0;
int?number?=?0,?newnumber?=?0;
printf("輸入整數(shù)個數(shù):");
scanf("%d",?n);
for(i?=?1;?i?=?n;?i++)
{
scanf("%d",?number);
newnumber?*=?10;????????????
newnumber?+=?getLastBit(number);????
}
printf("新整數(shù)是:%d\n",?newnumber);
return?0;?
}
int?getLastBit(int?number)
{
if(number?=?0)
{
printf("錯誤!\n");
exit(1);
}
else
return?number%10;
}
C語言中bit類型是“位”數(shù)據(jù)類型,一般在嵌入式軟件中應(yīng)用較多,用于IO口的控制,可以是單獨申明位變量,也可以申請位結(jié)構(gòu)數(shù)據(jù)類型,比如:
struct?st_flash?{???????????????????????????????????????????/*?struct?FLASH?*/
union?{???????????????????????????????????????/*?FLMCR1???????*/
unsigned?char?BYTE;????????????/*??Byte?Access?*/
struct?{??????????????????????????????????/*??Bit??Access?*/
unsigned?char?FWE?:1;??????/*????FWE???????*/
unsigned?char?SWE1:1;??????/*????SWE1??????*/
unsigned?char?ESU1:1;??????/*????ESU1??????*/
unsigned?char?PSU1:1;??????/*????PSU1??????*/
unsigned?char?EV1?:1;??????/*????EV1???????*/
unsigned?char?PV1?:1;??????/*????PV1???????*/
unsigned?char?E1??:1;???????/*????E1????????*/
unsigned?char?P1??:1;???????/*????P1????????*/
}??????BIT;
}?????????FLMCR1;
union?{?????????????????????????????????/*?FLMCR2???????*/
unsigned?char?BYTE;???????????????/*??Byte?Access?*/
struct?{?????????????????????????????????????/*??Bit??Access?*/
unsigned?char?FLER:1;??????/*????FLER??????*/
unsigned?char?SWE2:1;??????/*????SWE2??????*/
unsigned?char?ESU2:1;??????/*????ESU2??????*/
unsigned?char?PSU2:1;??????/*????PSU2??????*/
unsigned?char?EV2?:1;??????/*????EV2???????*/
unsigned?char?PV2?:1;??????/*????PV2???????*/
unsigned?char?E2??:1;??????/*????E2????????*/
unsigned?char?P2??:1;??????/*????P2????????*/
}??????BIT;
}?????????FLMCR2;
union?{?????????????????????????????????/*?EBR1?????????*/
unsigned?char?BYTE;???????????????/*??Byte?Access?*/
struct?{??????????????????????????/*??Bit??Access?*/
unsigned?char?EB7:1;???????/*????EB7???????*/
unsigned?char?EB6:1;???????/*????EB6???????*/
unsigned?char?EB5:1;???????/*????EB5???????*/
unsigned?char?EB4:1;???????/*????EB4???????*/
unsigned?char?EB3:1;???????/*????EB3???????*/
unsigned?char?EB2:1;???????/*????EB2???????*/
unsigned?char?EB1:1;???????/*????EB1???????*/
unsigned?char?EB0:1;???????/*????EB0???????*/
}??????BIT;
}?????????EBR1;
union?{?????????????????????????????????/*?EBR2?????????*/
unsigned?char?BYTE;???????????????/*??Byte?Access?*/
struct?{??????????????????????????/*??Bit??Access?*/
unsigned?char?EB15:1;??????/*????EB15??????*/
unsigned?char?EB14:1;??????/*????EB14??????*/
unsigned?char?EB13:1;??????/*????EB13??????*/
unsigned?char?EB12:1;??????/*????EB12??????*/
unsigned?char?EB11:1;??????/*????EB11??????*/
unsigned?char?EB10:1;??????/*????EB10??????*/
unsigned?char?EB9?:1;??????/*????EB9???????*/
unsigned?char?EB8?:1;??????/*????EB8???????*/
}??????BIT;
}?????????EBR2;
};
1?測試指定的位
unsigned?char?BitTst(unsigned?char?*Pa,char?b)//Pa為被測試的對象的地址,b值為指定的位
{
return?*Pa??(1b);
}
用法:
unsigned?char?x,y;
y=0x4F;
x=BitTst(y,3);
2?將指定的位置“1”
void?BitSet(unsigned?char?*Pa,char?b)//Pa為置位對象的地址,b值為指定的位
{
*Pa|=1b;
}
用法:BitSet(x,6);
3?將指定的位清“0”
void?BitClr(unsigned?char?*Pa,char?b)//Pa為清零位對象的地址,b值為指定的位
{
*Pa=~(1b);
}
用法:BitClr(x,6);
4?將指定的位取反
void?BitCpl(unsigned?char?*Pa,char?b)//Pa為取反位對象的地址,b值為指定的位
{
*Pa^=1b;
}