Oct 15, 2012 #1 M mohi@608 Member level 4 Joined Apr 4, 2012 Messages 69 Helped 1 Reputation 2 Reaction score 1 Trophy points 1,288 Activity points 1,719 i am trying to use bit set in one of my code but when i am using to_ulong command i am unable to get a desired result... the code is given below...... Code C - [expand]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 #include <iostream> #include <string> #include <bitset> using namespace std; class add { int r0,r1; public: int add_opcode(bitset<8> r0,bitset<8> r1); }; int add::add_opcode(bitset<8> r0,bitset<8> r1) { bitset<1> c,z,v; bitset<9> sum; int a,b; a=r0.to_ulong(); b=r1.to_ulong(); cout<<a<<"\n"<<b<<"\n"; sum=a+b; //r0=sum; cout<<r0<<"\n"; if(sum[8]==1) cout<<"c=1"<<"\n"<<" v=1"<<" \n"; else cout<<"c=0 "<<"\n"<<" v=0"<<"\n"; if(sum==0) cout<<" z="<<0<<"\n"; else cout<<" z="<<1<<"\n"; cout<<"sum= "<<sum<<endl; return 0; } int main() { add d1,d2,d3,d4; d1.add_opcode(00001111,00000000); d2.add_opcode(10001000,11111111); d3.add_opcode(0000,00000); d4.add_opcode(11111111,11111111); } pls check.... Last edited by a moderator: Oct 15, 2012
i am trying to use bit set in one of my code but when i am using to_ulong command i am unable to get a desired result... the code is given below...... Code C - [expand]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 #include <iostream> #include <string> #include <bitset> using namespace std; class add { int r0,r1; public: int add_opcode(bitset<8> r0,bitset<8> r1); }; int add::add_opcode(bitset<8> r0,bitset<8> r1) { bitset<1> c,z,v; bitset<9> sum; int a,b; a=r0.to_ulong(); b=r1.to_ulong(); cout<<a<<"\n"<<b<<"\n"; sum=a+b; //r0=sum; cout<<r0<<"\n"; if(sum[8]==1) cout<<"c=1"<<"\n"<<" v=1"<<" \n"; else cout<<"c=0 "<<"\n"<<" v=0"<<"\n"; if(sum==0) cout<<" z="<<0<<"\n"; else cout<<" z="<<1<<"\n"; cout<<"sum= "<<sum<<endl; return 0; } int main() { add d1,d2,d3,d4; d1.add_opcode(00001111,00000000); d2.add_opcode(10001000,11111111); d3.add_opcode(0000,00000); d4.add_opcode(11111111,11111111); } pls check....
Oct 15, 2012 #2 H horace1 Advanced Member level 5 Joined Nov 18, 2008 Messages 2,118 Helped 596 Reputation 1,188 Reaction score 571 Trophy points 1,393 Location Norwich, UK Activity points 13,063 in the statement Code: d1.add_opcode(00001111,00000000); the parameters are integers should the parameters be bitset<> Code: d1.add_opcode(bitset<8> (string("00001111")), bitset<8> (string("00000000"))); Last edited: Oct 15, 2012
in the statement Code: d1.add_opcode(00001111,00000000); the parameters are integers should the parameters be bitset<> Code: d1.add_opcode(bitset<8> (string("00001111")), bitset<8> (string("00000000")));
Oct 15, 2012 #3 M mohi@608 Member level 4 Joined Apr 4, 2012 Messages 69 Helped 1 Reputation 2 Reaction score 1 Trophy points 1,288 Activity points 1,719 well thanks for the help ...is it type casting what u are suggesting here??? and Code: int r0,r1; in the above code can i use Code: bitset<8> r0,r1; do we still require type casting ????
well thanks for the help ...is it type casting what u are suggesting here??? and Code: int r0,r1; in the above code can i use Code: bitset<8> r0,r1; do we still require type casting ????
Oct 15, 2012 #4 H horace1 Advanced Member level 5 Joined Nov 18, 2008 Messages 2,118 Helped 596 Reputation 1,188 Reaction score 571 Trophy points 1,393 Location Norwich, UK Activity points 13,063 mohi@608 said: well thanks for the help ...is it type casting what u are suggesting here??? and Code: int r0,r1; in the above code can i use Code: bitset<8> r0,r1; do we still require type casting ???? Click to expand... the bitset<> constructor https://www.cplusplus.com/reference/stl/bitset/bitset/ is used in the statement Code: d1.add_opcode(bitset<8> (string("00001111")), bitset<8> (string("00000000"))); you could use decimals, e.g. Code: d1.add_opcode(15, 0);
mohi@608 said: well thanks for the help ...is it type casting what u are suggesting here??? and Code: int r0,r1; in the above code can i use Code: bitset<8> r0,r1; do we still require type casting ???? Click to expand... the bitset<> constructor https://www.cplusplus.com/reference/stl/bitset/bitset/ is used in the statement Code: d1.add_opcode(bitset<8> (string("00001111")), bitset<8> (string("00000000"))); you could use decimals, e.g. Code: d1.add_opcode(15, 0);
Oct 15, 2012 #5 M mohi@608 Member level 4 Joined Apr 4, 2012 Messages 69 Helped 1 Reputation 2 Reaction score 1 Trophy points 1,288 Activity points 1,719 ok and i have another ques can i store the Code: cout<<"sum= "<<r0=sum<<endl; ?? as i am using the 8th bit for only checking and i want the result to be stored back in register r0.........
ok and i have another ques can i store the Code: cout<<"sum= "<<r0=sum<<endl; ?? as i am using the 8th bit for only checking and i want the result to be stored back in register r0.........
Oct 15, 2012 #6 H horace1 Advanced Member level 5 Joined Nov 18, 2008 Messages 2,118 Helped 596 Reputation 1,188 Reaction score 571 Trophy points 1,393 Location Norwich, UK Activity points 13,063 mohi@608 said: ok and i have another ques can i store the Code: cout<<"sum= "<<r0=sum<<endl; ?? as i am using the 8th bit for only checking and i want the result to be stored back in register r0......... Click to expand... bitset<> does not have an overloaded = operator however, there is a member function to_ulong(), e.g. Code: cout<<"sum= "<<(r0=sum.to_ulong())<<endl;
mohi@608 said: ok and i have another ques can i store the Code: cout<<"sum= "<<r0=sum<<endl; ?? as i am using the 8th bit for only checking and i want the result to be stored back in register r0......... Click to expand... bitset<> does not have an overloaded = operator however, there is a member function to_ulong(), e.g. Code: cout<<"sum= "<<(r0=sum.to_ulong())<<endl;
Oct 15, 2012 #7 M mohi@608 Member level 4 Joined Apr 4, 2012 Messages 69 Helped 1 Reputation 2 Reaction score 1 Trophy points 1,288 Activity points 1,719 thanks man....
Oct 15, 2012 #8 M mohi@608 Member level 4 Joined Apr 4, 2012 Messages 69 Helped 1 Reputation 2 Reaction score 1 Trophy points 1,288 Activity points 1,719 i have another question can we use pointers on bits and if yes is there any specific syntax for it ??
i have another question can we use pointers on bits and if yes is there any specific syntax for it ??
Oct 16, 2012 #9 H horace1 Advanced Member level 5 Joined Nov 18, 2008 Messages 2,118 Helped 596 Reputation 1,188 Reaction score 571 Trophy points 1,393 Location Norwich, UK Activity points 13,063 mohi@608 said: i have another question can we use pointers on bits and if yes is there any specific syntax for it ?? Click to expand... have a look at bitset::reference https://www.cplusplus.com/reference/stl/bitset/
mohi@608 said: i have another question can we use pointers on bits and if yes is there any specific syntax for it ?? Click to expand... have a look at bitset::reference https://www.cplusplus.com/reference/stl/bitset/
Oct 16, 2012 #10 M mohi@608 Member level 4 Joined Apr 4, 2012 Messages 69 Helped 1 Reputation 2 Reaction score 1 Trophy points 1,288 Activity points 1,719 well i have gone through the link but i am still not clear with it....i wish to give values by using pointers can i do that..... - - - Updated - - - well i have another doubt i wish to store a 16 bit num into 8 bit no. by selecting only last 8 bits .....
well i have gone through the link but i am still not clear with it....i wish to give values by using pointers can i do that..... - - - Updated - - - well i have another doubt i wish to store a 16 bit num into 8 bit no. by selecting only last 8 bits .....
Oct 16, 2012 #11 H horace1 Advanced Member level 5 Joined Nov 18, 2008 Messages 2,118 Helped 596 Reputation 1,188 Reaction score 571 Trophy points 1,393 Location Norwich, UK Activity points 13,063 as far as I am aware you cannot access bits in a bitset<> using pointers however, bitset::reference enables access to bits using the [] operator https://msdn.microsoft.com/en-us/library/xf3ah8t3.aspx e.g. Code: bitset<8> x(string("00001111")), y(string("00000000")); cout << "x " << x << endl; x[1]=~x[1]; cout << "x " << x << endl; when run gives Code: x 00001111 x 00001101
as far as I am aware you cannot access bits in a bitset<> using pointers however, bitset::reference enables access to bits using the [] operator https://msdn.microsoft.com/en-us/library/xf3ah8t3.aspx e.g. Code: bitset<8> x(string("00001111")), y(string("00000000")); cout << "x " << x << endl; x[1]=~x[1]; cout << "x " << x << endl; when run gives Code: x 00001111 x 00001101
Oct 16, 2012 #12 M mohi@608 Member level 4 Joined Apr 4, 2012 Messages 69 Helped 1 Reputation 2 Reaction score 1 Trophy points 1,288 Activity points 1,719 well i have another doubt i wish to store a 16 bit num into 8 bit no. by selecting only last 8 bits .....
well i have another doubt i wish to store a 16 bit num into 8 bit no. by selecting only last 8 bits .....
Oct 16, 2012 #13 H horace1 Advanced Member level 5 Joined Nov 18, 2008 Messages 2,118 Helped 596 Reputation 1,188 Reaction score 571 Trophy points 1,393 Location Norwich, UK Activity points 13,063 use the >> operator? Code: bitset<16> x(string("1010111100001111")); bitset<16> y(string("00000000")); cout << "x " << x << endl; y=x>>8; cout << "y " << y << endl; when run gives Code: x 1010111100001111 y 0000000010101111
use the >> operator? Code: bitset<16> x(string("1010111100001111")); bitset<16> y(string("00000000")); cout << "x " << x << endl; y=x>>8; cout << "y " << y << endl; when run gives Code: x 1010111100001111 y 0000000010101111
Oct 16, 2012 #14 M mohi@608 Member level 4 Joined Apr 4, 2012 Messages 69 Helped 1 Reputation 2 Reaction score 1 Trophy points 1,288 Activity points 1,719 well for pointer i was doing this Code: bitset<8> acc; int *ptr_acc=&acc; and rest of the arithmetic was done on acc but it is still nt working...... - - - Updated - - - can we use signed representation in bit form and how ???.........
well for pointer i was doing this Code: bitset<8> acc; int *ptr_acc=&acc; and rest of the arithmetic was done on acc but it is still nt working...... - - - Updated - - - can we use signed representation in bit form and how ???.........
Oct 16, 2012 #15 H horace1 Advanced Member level 5 Joined Nov 18, 2008 Messages 2,118 Helped 596 Reputation 1,188 Reaction score 571 Trophy points 1,393 Location Norwich, UK Activity points 13,063 mohi@608 said: well for pointer i was doing this Code: bitset<8> acc; int *ptr_acc=&acc; and rest of the arithmetic was done on acc but it is still nt working...... Click to expand... not sure what you are trying to do? mohi@608 said: can we use signed representation in bit form and how ???......... Click to expand... bitset<> stores bits what they represent is up to you e.g. Code: bitset<16> z(string("0")); cout << "z " << z << endl; short int a=-1; z=a; cout << "z " << z << endl; stored a 16-bit signed int in a bitset<> when run it gives Code: z 0000000000000000 z 1111111111111111
mohi@608 said: well for pointer i was doing this Code: bitset<8> acc; int *ptr_acc=&acc; and rest of the arithmetic was done on acc but it is still nt working...... Click to expand... not sure what you are trying to do? mohi@608 said: can we use signed representation in bit form and how ???......... Click to expand... bitset<> stores bits what they represent is up to you e.g. Code: bitset<16> z(string("0")); cout << "z " << z << endl; short int a=-1; z=a; cout << "z " << z << endl; stored a 16-bit signed int in a bitset<> when run it gives Code: z 0000000000000000 z 1111111111111111