Passing a value from member function to non member function

I'm trying to write a program that manipulates the bits of integers in a vector.

Down below you can see the GetSet() function and the ToBinary() function which are memeber and non member functions respectively. the return value of getset needs to be passed to tobinary so that tobinary can print out the set (which is an integer in the vector) in a binary format.

I'm confused on how i can pass the value to tobinary when it is a non member function and getset is a member function.

Any ideas would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>

using namespace std;

//1 in the 0th bit = 1
//1 in the 1st bit = 2
                   //4
                   //8
                   //16
                   //32
                   //64
                   //128     

// << left shift
// >> right shfit
// & and
// | or 
// ^ xor

const int N_BITS = sizeof(int) * 8;

class BITSET
{
    private:
        vector <int> bits;
         
    public:
        BITSET();
        bool Test(int i) const; //testing bits
        void Set(int idx); //set to 1
        void Clear(int idx); //set to 0
        int GetNumSets() const; //get amt of sets.
        int GetSet(int idx) const; //bits.at()

};

string ToBinary(int value, int spacing); //func def down below.


void UserInterface(){
    cout << "t <index_of_bit> to test a bit" << endl;
    cout << "s <bit> to set a bit" << endl;
    cout << "c <bit> to clear a bit" << endl;
    cout << "g <set> to print the binary representation of desired set" << endl;
    cout << "n to view the number of sets in the bitset" << endl;
    cout << "q to quit :(" << endl;
}


BITSET::BITSET() { 
    
    bits.resize(1, 0);
    
    //CONSTRUCTOR
    //INITIALIZE VECTOR TO SIZE 1 WITH VALUE 0
}

int main(){
   
    BITSET set;
    int value;
    int spacing;
    char option;
    int idx;
    
    while(option != 'q'){
        cout << endl;
        cout << "Welcome to the User Interface please review our options: " << endl;
        cout << endl;
        UserInterface();
        cout << endl;
        cout << "Select your desired option: ";
    
    cin >> option;
    cout << endl;
    switch (option){
        case 't':
            set.Test(idx);
            cout << endl;
            break;
        case 'g': 
            set.GetSet(idx);
            ToBinary(value, spacing);
            break;
        case 's': 
            set.Set(idx);
            break;
        case 'n':
            set.GetNumSets();
            break;
        case 'c':
            set.Clear(idx);
            break;
        case 'f':
            set.GetSet(idx);
            break;
    }   
}
    return 0;

    
}

int BITSET::GetSet(int idx) const
{
    cout << "Enter the set you'd like to view: ";
    cin >> idx;
    return bits.at(idx);
}

void BITSET::Clear(int idx){
    int x = 1;
    cout << "Enter the index of the bit you would like to clear: ";
    cin >> idx;
    bits.at(idx / N_BITS) ^= (x << (idx - 1));
    cout << endl;
    cout << "Integer representation of the number after clearing the bit: " << bits.at(idx / N_BITS);
    cout << endl;
}
bool BITSET::Test(int idx) const{
    // &, >>

    cout << "Enter the index of the integer you'd like to test: ";
    cin >> idx;
    
    if (((bits[idx & 32]) | (idx >> 5)) > 0) {
     printf("1");
     return true;
    }   
    else {
     printf("0");
     return false;
    }

    
}

void BITSET::Set(int idx){
    // <<, |
    
    
    //getnumsets * 32 if greater than index then you're fine 
    //otherwise resize vector

    int x = 1;
    cout << "Enter the index of the bit you would like to set: ";
    cin >> idx;
    cout << endl;

    if ((GetNumSets() * N_BITS) > idx){
        bits.at(idx / N_BITS) |= (x << (idx - 1));
        cout << "Integer representation of number after the set: " << bits.at(idx / N_BITS);
        cout << endl;
    }

    else{
        bits.resize(bits.size() + (idx % N_BITS));
    }
    
}

int BITSET::GetNumSets() const {
    
    cout << bits.size();
    cout << endl;
    return bits.size();
}

string ToBinary(int value, int spacing){
    
    unsigned int mask = pow(2, (N_BITS - 1)); //Mask defined at beginning of program, thanks Woz
    spacing = 4;
    cout << endl;
    int idx = 0;
    while (mask > 0)
    {
        if((value & mask) != 0)
            cout << "1";
        else
            cout << "0";
        mask = (mask >> 1);

        idx++;

        if(idx == spacing)
        {    
            cout << " ";
            idx = 0;
        }
    }
    return "string";
}
I don't understand the problem. Just call the function like usual.

BTW, you should use unsigned int to hold your bits in the vector.

1
2
3
4
5
6
7
8
void ToBinary(unsigned value, unsigned spacing = 4)
{
    for (unsigned i = 0, mask = 1u << (N_BITS - 1); mask; mask >>= 1)
    {
        cout << (value & mask ? '1' : '0');
        if (++i % spacing == 0) cout << ' ';
    }
}

Last edited on
The problem is that ToBinary() can't accept values from GetSet() because it's not included in the class. I can set a bit and then GetSet() will give us the index in the vector after it's been edited. then i need to pass that index into ToBinary() so it'll print out that specific index in the vector when I call ToBinary() with said index.
The problem is that ToBinary() can't accept values from GetSet() because it's not included in the class.

Of course it can.
Maybe something like:

 
ToBinary(set.GetSet(idx));

BTW, you shouldn't pass 'spacing' in if you are just going to treat it like a local variable of the function.
Look at my rewrite above.
And don't use pow to calculate an integer power of 2. Use left-shift instead.
Last edited on
Thank you @dutch got it working
Topic archived. No new replies allowed.