Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing Binary number in Integer variable

Tags:

c++

arrays

binary

I am making a simulator, which simulates the method of multiplication of a processor. The thing is first I have to convert the decimal form to binary, each bit of binary representation is stored at separate location in an array. Now the multiplication of two binary numbers, stored in arrays as mentioned above is very complex and also very memory consuming.

because I have to follow this method;

1000

x

1110


   `0000`

  `1000`

 `1000`

`1000`

= 1110000

I know that I can easily do this by multiplying decimals forms together and then convert them to binary but unfortunately that's not required here.

I was thinking if there is a way to store the binary number stored in array as a single integer containing binary no. Or any other easy way for multiplying binary bits stored in array. For Example:

a[0]=1,a[1]=1, .... ,a[32]=0

so I want the integer variable to contain

int x=110....0

Is there a way to do this?

Regards

like image 535
Alfred Avatar asked Jul 24 '26 00:07

Alfred


1 Answers

You can use std::bitset

std::bitset<32> b;

b[0] = 1;
b[1] = 1;
...
like image 114
John Bandela Avatar answered Jul 26 '26 14:07

John Bandela



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!