【发布时间】:2013-11-05 14:09:38
【问题描述】:
我正在尝试从 16 位二进制字符串中提取 4 位,即从一个单词中剔除 谁能告诉我这个程序有什么问题?
#include <sstream>
#include <iomanip>
#include <math.h>
#include<iostream>
using namespace std;
int main()
{
std::string aBinaryIPAddress = "1100110011001100";
std::string digit0 = aBinaryIPAddress & 0x0f;
cout << "digit0 : " << digit0 << endl;
std::string digit1 = (aBinaryIPAddress >> 4) 0x0f;
cout << "digit1 : " << digit1 << endl;
std::string digit2 = (aBinaryIPAddress >> 8) 0x0f;
cout << "digit2 : " << digit2 << endl;
std::string digit3 = (aBinaryIPAddress >> 12) 0x0f;
cout << "digit3 : " << digit3 << endl;
return 0;
}
我收到以下错误:
changes.cpp: In function `int main()':
changes.cpp:117: error: invalid operands of types `char*' and `int' to binary `
operator>>'
changes.cpp:117: error: parse error before numeric constant
【问题讨论】:
-
尝试使用
std::bitset或二进制文字。 -
是什么让您认为您可以在
std::string上执行整数位运算? -
aBinaryIPAddress不是二进制字符串。它是一个字符串。 -
@akonsu:不仅如此,它还是一个封装了字符串抽象的对象。
-
@LightnessRacesinOrbit 那么,我应该使用什么?
标签: c++ type-conversion word nibble