How can i change this bit?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <bitset>
#include <iostream>

using namespace std;

int main()
{int a=25,b;
bitset<8>binarya(a);
cout<<binarya<<endl;
binarya[4]=0;
cout<<binarya<<endl;
bitset<8>binaryb(b);
binaryb=binarya;
binaryb[4]=1;
cout<<a<<endl;
cout<<b<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

How can i change this a value's 4th bit?Bitset is not changing.
closed account (DSLq5Di1)
binarya is constructed with a copy of a, changes to one will not be reflected in the other.

A roundabout way of doing what you expected:-
1
2
3
4
5
6
7
8
int a = 25;
cout << bitset<8>(a) << " " << a << endl;

bitset<8> binarya(a);
binarya[4] = 0;

a = binarya.to_ulong();
cout << bitset<8>(a) << " " << a << endl;

00011001 25
00001001 9
Thank u for ur help.
Topic archived. No new replies allowed.