HDU1196 Lowest Bit,hdu1196lowestbit
Lowest BitTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7728 Accepted Submission(s): 5674
Problem DescriptionGiven an positive integer A (1 <= A <= 100), output the lowest bit of A.
For example, given A = 26, we can write A in binary form as 11010, so the lowest bit of A is 10, so the output should be 2.
Another example goes like this: given A = 88, we can write A in binary form as 1011000, so the lowest bit of A is 1000, so the output should be 8.
InputEach line of input contains only an integer A (1 <= A <= 100). A line containing "0" indicates the end of input, and this line is not a part of the input data.
OutputFor each A in the input, output a line containing only its lowest bit.
Sample Input
26880
Sample Output
28
樹狀數組要用到這個知識點。
#include <stdio.h>int main(){int n;while(scanf("%d", &n), n){printf("%d\n", n & (-n));}return 0;}
想教下面程式m&-m這是什貌似還有">>"這個也是與2進位有關的,解釋這2個問題
n=m&-m;//這個表示m的二進位表示的補碼同-m二進位的補碼相"與",電腦中的數值都是用補碼錶示
比如輸入的值為24
他的二進位是00011000補碼也是00011000
-24的二進位表示為10011000,它的補碼為他的反碼加1變為11101000
然後00011000&11101000就變為00001000轉換為10進位就是數值8
>>這個符號表示二進位是時候是右移,對於26來說26>>1,表示26右移一位,變為00001100轉換成十進位的為12. 右移的話是8進位都向右移動一位,高位補0,對於負數來數,高位是符號位不變,其他位向右移動
用245驅動DS18B20的程式
//本人親自調試過,包你沒問題
//外部晶振11.0592M
#include<stc12c5a60s2.h>
bit B20DIRCTR=P3^7; //245方向控制端 H=A->B,L=B->A
sbit DQ=P2^7; //DS18B20資料口
void DS18B20_delayms(unsigned int z)
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void DS18B20_delayus(unsigned int us)
{
while(--us);
}
/*-------------------------
*****DS18B20 operation*****
-------------------------*/
/*initialization*/
bit DS18B20_reset()
{
bit presence=0;
B20DIRCTR=1;
DQ=1;
DS18B20_delayus(10);
DQ=0;
DS18B20_delayus(500);
DQ=1;
DS18B20_delayus(60);
B20DIRCTR=0;
DS18B20_delayus(2);
if(DQ)
presence=0;
else
presence=1;
B20DIRCTR=1;
return presence;
}
#if 0
/*read a bit*/
bit DS18B20_readbit()
{
bit dat;
B20DIRCTR=1;
DQ=0;
DS18B20_delayus(1);
DQ=1;
DS18B20_delayus(5);
B20DIRCTR=0;
dat=DQ;
DS18B20_delayus(100);
B20DIRCTR=1;
return dat;
}
/*read byte*/
unsigned char DS18B20_readbyte()
{
unsigned char i,j,dat;
dat=0;
for(i=0;i<8;i++)
{
j=DS18B20_readbit(); //The lowest bit at front
dat=(j<<7)|(dat>>1); //The positive preface arranges a word knot potential
}
return dat;
}
#endif
unsigned char DS18B20_readbyte()
{
unsigned char i,temp;
for(i=1;i<=8;i++)
{
temp>>=1;
B20DIRCTR=1;
DQ=0;
DS18B20_delayus(1);
DQ=1;
B20DIRCTR=0;
if(DQ)
temp|=0x80;
DS18B20_delayus(45);
}
B20DIRCTR=1;
return temp;
}
/*write byte*/
void DS18B20_wr......餘下全文>>