想请教高手下面程序m&-m这是什么意思啊~貌似还有">>"这个也是与求2进制有关的,求高手解释这2个问题啊~!

HDOJ 1196
http://acm.hdu.edu.cn/showproblem.php?pid=1196

Problem Description
Given 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.

Input
Each 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.

Output
For each A in the input, output a line containing only its lowest bit.

Sample Input
26
88
0

Sample Output
2
8

#include <iostream>
using namespace std;

int main()
{
int m,n;
while(cin>>m&&m)
{
n=m&-m;
cout<<n<<endl;
}
return 0;
}

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,对于负数来数,高位是符号位不变,其他位向右移动追问

对于8进制和负数能举几个例子吗~谢谢

追答

比如-26怎么求它的补码呢
第一步:写出26的的二进制原码表示为:00011010,补码不变也是00011010
第二步:写出-26的二进制原码表示为:10011010其中最高位的1表示它是负数
第三步:写出-26的二进制反码表示为:11100101
第四步:写出-26的二进制补码表示为:11100101+1变为11100110
然后26&-26=00011010&11100110=00000010(按位取&得结果)用十进制表示就是2

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-04-19
cin,m分别代表什么?
大家正在搜