Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

bitset in c++ related issues

Status
Not open for further replies.

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:

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:
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 ????
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);
 

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.........
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;
 
i have another question can we use pointers on bits and if yes is there any specific syntax for it ??
 

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 .....
 

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
 

well i have another doubt i wish to store a 16 bit num into 8 bit no. by selecting only last 8 bits .....
 

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
 

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......
not sure what you are trying to do?

can we use signed representation in bit form and how ???.........
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
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top