满200减50什么意思:C++十进制怎样转化为二进制

来源:百度文库 编辑:高考问答 时间:2024/05/06 05:11:02

C++中的函数 IntToBin
Returns a binary string representation for an Integer value.

function IntToBin(Value: cardinal): string;

Parameters

Value: cardinal

Cardinal value to be converted.

Returns

String - Binary representation of the Integer value.

Description

IntToBin is a String function used to construct a binary representation of a 32-bit Integer value. The return value for IntToBin will contain a string of "0" or "1" characters for each of the bits in the integer Value.

如果是求算法:
如下:
int k;//需要转换的十进只数.
int t=k;
bool muls=false;
if(k>=0)
musl=false; //判断是正负数.

char *Bin; //保存转换得到得2进制数组
int i=0;
for(;;)
{
if((t=t/2)!=0)
i++;
else
break;
}

Bin=new char[i+1];
t=k;
for(int j=i;j>0;j--)
{
Bin[j]=t%2;
t=t/2;
}

Bin[0]=(int)muls; //最高位为符号位.

数的进制转换,最方便的方法是用一个栈来实现,栈是数据结构里的基本结构,就不写它的实现了

void main(){
stack S; // 栈
int num; // 需要转换的十进制数字

cin>>num;

while(num){
S.push(num%2);
num = num/2;
}

while (S.empty()){
cout<<S.pop();
}
}

cout<<binary<<a;