1

I am trying to extract 4-bits from a 16-bit binary string, i.e nibbles out of a word Can anyone tell me what is wrong with this program?

#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;
}

I am getting the following error:

 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
Prats
  • 1,515
  • 6
  • 16
  • 30

5 Answers5

2

If you are manipulating a string, you should be using substr, not "shift and mask" technique: & and >> operators are undefined for strings and ints.

Here is how to do it with substr:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string aBinaryIPAddress = "0101100111101101";
    size_t len = aBinaryIPAddress.size();
    for (int i = 0 ; i != 4 ; i++) {
        cout << "Digit " << i << ": " << aBinaryIPAddress.substr(len-4*(i+1), 4) << endl;
    }
    return 0;
}

This prints

Digit 0: 1101
Digit 1: 1110
Digit 2: 1001
Digit 3: 0101

Demo on ideone.

If you need four individual variables, "unroll" the loop, like this:

string d0 = aBinaryIPAddress.substr(len-4, 4);
string d1 = aBinaryIPAddress.substr(len-8, 4);
string d2 = aBinaryIPAddress.substr(len-12, 4);
string d3 = aBinaryIPAddress.substr(len-16, 4);
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thanks for this dude, but I am having a group of binary words i,e 16-bits number stored in a char pointer around 128-bits – Prats Nov 05 '13 at 14:26
  • So for each word I need to extract a nibble, it will not be possible to use sub string on each word – Prats Nov 05 '13 at 14:27
  • @Prats Why not? Make a `string` out of your `char*` (assuming that it is formatted correctly), and then proceed with `substr`. – Sergey Kalinichenko Nov 05 '13 at 14:28
  • How can I take each of these digit0, digit1, digit3,.. into variables each time I extract from a word, I need to do furthur calculations on these nibbles. – Prats Nov 05 '13 at 14:31
  • @Prats If you need to take them into individual variables, replace `i` with a number `0`, `1`, `2`, or `3`, and use `string d0 = aBinaryIPAddress.substr(len-4, 4)`, `string d1 = aBinaryIPAddress.substr(len-8, 4)`, `string d2 = aBinaryIPAddress.substr(len-12, 4)`, etc. – Sergey Kalinichenko Nov 05 '13 at 14:34
  • Could you please tell how? – Prats Nov 05 '13 at 14:37
  • Say, if I have two words i.e, 32-bits string, than will I have two for loops. One for extracting words and the other for extracting nibble? – Prats Nov 05 '13 at 14:45
  • @Prats You can do it this way, but that is not necessary: you can continue on with the sequence of `substr` arguments - `len-16`, `len-20`, `len-24`, `len-28`, and `len-32` to extract all nibbles without an outer loop. – Sergey Kalinichenko Nov 05 '13 at 14:53
  • Thanks dude for giving a solution, I was thinking the other way – Prats Nov 05 '13 at 14:58
  • Also, is it safe extracting nibbles using string operations or using bitwise operators? – Prats Nov 05 '13 at 14:58
  • Hey, what if I want to extract nibbles from first not from the last? – Prats Nov 05 '13 at 15:02
  • 1
    @Prats Then instead of going backwards (i.e. `substr(len-4, 4)`, `substr(len-8, 4)`, ...) you'd go forward (i.e. `substr(0, 4)`, `substr(4, 4)`, `substr(12, 4)`, and so on). – Sergey Kalinichenko Nov 05 '13 at 15:07
0

You can not convert from an integer to string the way you do. You can use stringstream:

#include <iomanip>
......
std::string convert_int_to_hex_stiring(int val) {
  std::stringstream ss;
  ss << std::hex << val;
  return val;
}

Before you use the function above you will have to read integers from parts of the input string. Problem is that the bitwise operations are not defined on a string.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
0

the type of aBinaryIPAddress should be a number rather than a string

something like

unsigned int aBinaryIPAddress = 0b1100110011001100;

should work

Alix Martin
  • 332
  • 1
  • 5
0

You have a std::string object that holds 16 characters, each with the value '0' or '1'. If you want to look at "nibbles" taken from there, just pull out the 4-character groups that you need:

std::string digit0 = aBinaryIPAddress.substr(12,4);
std::cout << digit0 << '\n';

That's just text manipulation; if you want to get values, it's straightforward (and a useful exercise) to convert the characters in digit0 into a numeric value..

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
-1

It is obvious that the problem is in statements like this

std::string digit0 = aBinaryIPAddress & 0x0f;

Do you understand that std::string is not a number?!

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Yes I understand that string is not a number, but how to use it for extracting a nibble – Prats Nov 05 '13 at 14:22