怎样将二进制补码转化为原码?

paradox_xia 2012-04-25 01:13:33
如,怎样将二进制补码1000001111000011010010010,0000101011110000101011110001010,101010111110101010001,01010100001111等化为原码的形式?
有现成的函数能直接转换吗?
...全文
1492 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
evencoming 2012-04-26
  • 打赏
  • 举报
回复
[code=C/C++]
#include<climits>
#include<string>
#include<iostream>
#include<bitset>
using namespace std;
#define BINARY_BIT (20)
#define BINARY_SIGN (1<<(BINARY_BIT-1))
#define BINARY_MASK (~-(1<<BINARY_BIT))
string true_form_long(const string& complement)
{
bitset<sizeof(long)*8> bits(complement);
unsigned long val = bits.to_ulong();
if(val > (unsigned long)(BINARY_SIGN-1))
{
val=(val&(BINARY_SIGN-1));
return bitset<sizeof(long)*8>(((~val+1)|BINARY_SIGN)&BINARY_MASK).to_string().substr(32-BINARY_BIT);
}
if(complement.size()<BINARY_BIT)
{
string s(BINARY_BIT-complement.size(),'0');
return s+complement;
}
return complement;
}
string not_code(const string & complement)
{
bitset<sizeof(long)*8> bits(complement);
unsigned long val = bits.to_ulong();
return bitset<sizeof(long)*8>((~val)&BINARY_MASK).to_string().substr(32-BINARY_BIT);
}
void test_long(long val)
{
cout<<"补码数字:"<<val<<"\n";
bitset<32> a(val);
cout<<"补码:"<<a.to_string().substr(32-BINARY_BIT)<<"\n";
cout<<"原码:"<<true_form_long(a.to_string())<<"\n";
cout<<"反码:"<<not_code(a.to_string())<<endl;
// bitset<32> b(true_form_long(a.to_string()));
// cout<<b.to_string()<<endl;
}
void test_string(const string& str)
{
if(str.size()==32)
cout<<"补码:"<<str.substr(32-BINARY_BIT)<<"\n";
else
{
string s(BINARY_BIT-str.size(),'0');
cout<<"补码:"<<s+str<<endl;
}
cout<<"原码:"<<true_form_long(str)<<"\n";
cout<<"反码:"<<not_code(str)<<endl;
}
int main()
{
string str="1000001111000011010010010";
bitset<32> a(str);
cout<<a.to_ulong()<<endl;
cout<<true_form_long(str)<<endl;
for(long i=-10;i<0;i++)
{
test_long(i);
cout<<endl;
}
puts("输入补码");
string s;
cin>>s;
test_string(s);
}

[/CODE]
paradox_xia 2012-04-26
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 的回复:]

引用 11 楼 的回复:
引用 10 楼 的回复:

引用 8 楼 的回复:
引用 7 楼 的回复:

引用 6 楼 的回复:

引用 3 楼 的回复:

C/C++ code

#include<climits>
#include<string>
#include<iostream>
#include<bitset>
string true_form_long……
[/Quote]
那个 1001...作为20位的数据。
是作为string输入。
最后得到 "010110……"这样的字符串
一个输入输出的例子:
输入:10101111110000110010
输出:01010000001111001101
evencoming 2012-04-26
  • 打赏
  • 举报
回复
9L的代码是将一个 "1001"这样的字符串(补码) 按照 32位二进制转换成原码,以字符串保存的.
evencoming 2012-04-26
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]
引用 10 楼 的回复:

引用 8 楼 的回复:
引用 7 楼 的回复:

引用 6 楼 的回复:

引用 3 楼 的回复:

C/C++ code

#include<climits>
#include<string>
#include<iostream>
#include<bitset>
string true_form_long(const string&am……
[/Quote]
告诉我你那个 1001...作为多少位的数据,16?24?32?还是多少?
怎么输入,是作为字符串,还是 一个int的数据还是什么?
最后你要得到什么数据? "010110"这样的字符串还是?
然后给一个输入输出的例子.

paradox_xia 2012-04-26
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

引用 8 楼 的回复:
引用 7 楼 的回复:

引用 6 楼 的回复:

引用 3 楼 的回复:

C/C++ code

#include<climits>
#include<string>
#include<iostream>
#include<bitset>
string true_form_long(const string&amp;amp;amp;……
[/Quote]我其实只要把"1000001111000011010010010"之类的转化为反码就行。这样还是不行啊。怎么办?
evencoming 2012-04-26
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]
引用 7 楼 的回复:

引用 6 楼 的回复:

引用 3 楼 的回复:

C/C++ code

#include<climits>
#include<string>
#include<iostream>
#include<bitset>
string true_form_long(const string&amp;amp;amp; complement)
{……
[/Quote]
见8L,之前有个地方搞错了..忘了计算机本身就是存储的补码.
evencoming 2012-04-26
  • 打赏
  • 举报
回复
[code=C/C++]
#include<climits>
#include<string>
#include<iostream>
#include<bitset>
using namespace std;
string true_form_long(const string& complement)
{
bitset<sizeof(long)*8> bits(complement);
unsigned long val = bits.to_ulong();
if(val > (unsigned long)LONG_MAX)
{
val=val&0x7fffffff;
return bitset<sizeof(long)*8>(~val+1).to_string();
}
return complement;
}
void test_long(long val)
{
cout<<"补码数字:"<<val<<"\n";
bitset<32> a(val);
cout<<"补码:"<<a.to_string()<<"\n";
cout<<"原码:"<<true_form_long(a.to_string())<<"\n";
bitset<32> b(true_form_long(a.to_string()));
cout<<b.to_string()<<endl;
}
int main()
{
string str="1000001111000011010010010";
bitset<32> a(str);
cout<<a.to_ulong()<<endl;
cout<<true_form_long(str)<<endl;
for(long i=-10;i<10;i++)
{
test_long(i);
cout<<endl;
}
}

[/CODE]
paradox_xia 2012-04-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

引用 6 楼 的回复:

引用 3 楼 的回复:

C/C++ code

#include<climits>
#include<string>
#include<iostream>
#include<bitset>
string true_form_long(const string&amp;amp; complement)
{
bitset<sizeof(……
[/Quote]放了,就是同样的输出来,你也可以试下.
qq120848369 2012-04-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

引用 3 楼 的回复:

C/C++ code

#include<climits>
#include<string>
#include<iostream>
#include<bitset>
string true_form_long(const string&amp; complement)
{
bitset<sizeof(long)*8> bits(comple……
[/Quote]

正数的补码就是自身,你放个负数进去看看。
paradox_xia 2012-04-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

C/C++ code

#include<climits>
#include<string>
#include<iostream>
#include<bitset>
string true_form_long(const string& complement)
{
bitset<sizeof(long)*8> bits(complement);
unsigned lon……
[/Quote]试了,没有变为原码,数根本就没变
paradox_xia 2012-04-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

C/C++ code

#include<climits>
#include<string>
#include<iostream>
#include<bitset>
string true_form_long(const string& complement)
{
bitset<sizeof(long)*8> bits(complement);
unsigned lon……
[/Quote]加一语句using namespace std;
paradox_xia 2012-04-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

C/C++ code

#include<climits>
#include<string>
#include<iostream>
#include<bitset>
string true_form_long(const string& complement)
{
bitset<sizeof(long)*8> bits(complement);
unsigned lon……
[/Quote]1>

转化完的原码是string型吧?

c:\users\xia\documents\visual studio 2005\projects\test3\test3\test3.cpp(6) : error C2146: 语法错误 : 缺少“;”(在标识符“true_form_long”的前面)
1>c:\users\xia\documents\visual studio 2005\projects\test3\test3\test3.cpp(6) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>c:\users\xia\documents\visual studio 2005\projects\test3\test3\test3.cpp(6) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>c:\users\xia\documents\visual studio 2005\projects\test3\test3\test3.cpp(6) : error C2143: 语法错误 : 缺少“,”(在“&”的前面)
1>c:\users\xia\documents\visual studio 2005\projects\test3\test3\test3.cpp(7) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
paradox_xia 2012-04-26
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 的回复:]

C/C++ code

#include<climits>
#include<string>
#include<iostream>
#include<bitset>
using namespace std;
#define BINARY_BIT (20)
#define BINARY_SIGN (1<<(BINARY_BIT-1))
#define BINARY_MASK (~-(1<<BIN……
[/Quote]再问一个问题,如何将6位的二进制数变成ASCII码?
paradox_xia 2012-04-26
  • 打赏
  • 举报
回复
谢谢楼上所有人
evencoming 2012-04-25
  • 打赏
  • 举报
回复
[code=C/C++]
#include<climits>
#include<string>
#include<iostream>
#include<bitset>
string true_form_long(const string& complement)
{
bitset<sizeof(long)*8> bits(complement);
unsigned long val = bits.to_ulong();
if(val > (unsigned long)LONG_MAX)
{
return bitset<sizeof(long)*8>(~val+1).to_string();
}
return complement;
}
int main()
{
string str="1000001111000011010010010";
bitset<32> a(str);
cout<<a.to_ulong()<<endl;
cout<<true_form_long(str)<<endl;
}
[/CODE]
qq120848369 2012-04-25
  • 打赏
  • 举报
回复
~n+1
return__0 2012-04-25
  • 打赏
  • 举报
回复
正数的补码与该数的原码相同,十进制:10 二进制:00001010 原码:00001010 补码:00001010
负数的补码取反+1 十进制:-10 二进制:10001010 原码:10001010 补码:11110110
上面的取反符号位不变。
把补码取反+1就是原码了。
自己动手编一个,很简单。。

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧