0
class Address {

    private :

        unsigned char arr[4] = {0};
        bitset<8>  bits[4];

    public :

        Address(){
            char  ip[50];
            char temp[4];
            cout <<"Enter your IP ADDRESS";
            cin >>ip;
            int i;
            int k=0;
            for(i=0;ip[i]!='\0';i++){

               if(ip[i]!='.') arr[k]=arr[k]*10 + (ip[i]-48);
               else k++;

            //Easy PARSE
            }
        }
};

I need to implement an Address class for IPV4. When I call the constructor of my Address class I am taking-> parsing the input to an 8-bit array of 4, which is basically the 4 octets of IPV4. 255.255.255.255 For example. Now I need to convert it to a binary array of 4. I don't want to create an external binary converter for this. I am willing to implement it using a bitset. Now the issue is, I haven't found a bitset function which lets me initialize the bits(convert from decimal to bitset) apart from its constructor, which is called as soon as my Address class is called. I can do it by having 4 separate member pointers to type bitset<8> and allocating each in Address(), but that is very inelegant approach. Any ideas? :/

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Nagato
  • 87
  • 2
  • 8
  • I don't understand the need for the binary representation. Why not just use then 4 unsigned chars? They can directly store the bit patters for 0-255 for each octet. After all, a byte is essentially an octet. – ttemple May 02 '18 at 10:27
  • Side-note: This wouldn't be so much of a problem if you weren't misusing the constructor. A constructor that takes no arguments and demands input from `cin` is poorly designed; it's not reusable or composable in any meaningful way. If you want to define a constant `Address` representing `127.0.0.1`, this constructor is useless. It's similarly useless if you want to read from a file handle aside from `cin`, a database, etc. As a rule, interacting with `cin`/`cout` should be done solely via your program's `main` function (plus factored out functions existing solely to support `main`). – ShadowRanger May 02 '18 at 17:51
  • Basically, [you have an `XY` problem](https://meta.stackexchange.com/q/66377/322040); a properly designed constructor could initialize your `bitset`s correctly from the start (before you reach the constructor body) from provided argument(s), so you're default-initializing them only to replace the values in the constructor body. Reading `Address`es would be done via [overloading `operator>>` for streams](https://stackoverflow.com/a/4421719/364696), then having `main` send the prompt to `cout` and read into an existing `Address` via `cin >> addr;`, separating I/O from structure definitions. – ShadowRanger May 02 '18 at 17:56

2 Answers2

1

You can change the bitset using the assignment operator after the construction.

This might be the part you are looking for:

for(int i=0; i <4; i++) {
    bits[i] = bitset<8>(arr[i]);
}
gchen
  • 1,173
  • 1
  • 7
  • 12
1

How about something like this:

#include <iostream>
#include <bitset>

int main()
{
    std::bitset<8> bits[4]; 
    std::cout << "Enter IP: ";

    for (auto &bit : bits)
    {
        unsigned long n;
        std::cin >> n;
        bit = n;
        std::cin.ignore();
    }

    // test
    std::cout << std::endl;

    for (auto const &bit : bits)
        std::cout << bit.to_ulong() << " ";
    std::cout << std::endl;

    for (auto const &bit : bits)
        std::cout << bit.to_string();
    std::cout << std::endl;
}

https://ideone.com/AcvBvf

192 168 0 1 
11000000101010000000000000000001
Killzone Kid
  • 6,171
  • 3
  • 17
  • 37