Docsity

Binary Operations and Multiplication Algorithms - Prof. T. Leyk, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
1.
(a)
(i)
int x;
cin>>x;
cout<<bitset<numeric_limit<short>::digits>(x)<<endl;
(ii)
cout<<”27 as binary=”
bitset<numeric_limit<short>::digits>(27)<<endl;
cout<<”133 as binary=”
bitset<numeric_limit<short>::digits>(133)<<endl;
(iii)
arithmetic operations=3*x
(vi)
string bin_num;
cout<<”Input a binary number as a string”<<endl;
cin>>bin_num;
cout << bin_num” as number: "
<< bitset<100>(bin_num).to_ulong() << endl;
(b)
(i) 16 bits in both integral and binary representation sum.
(ii) string stral, stral1;
int n;
cout<<"enter a binary number to add"<<endl;
cin>>stral;
cin>>stral1;
cout<<"enter a number of bits"<<endl;
cin>>n;
bitset<n>bitvec1(stral+stral1);
cout<<bitvec1;
(iii) 4*n
(c)
(i) 16 bits in both
pf3

Partial preview of the text

Download Binary Operations and Multiplication Algorithms - Prof. T. Leyk and more Assignments Data Structures and Algorithms in PDF only on Docsity!

(a) (i) int x; cin>>x; cout<<bitset<numeric_limit::digits>(x)<<endl; (ii) cout<<”27 as binary=” bitset<numeric_limit::digits>(27)<<endl; cout<<”133 as binary=” bitset<numeric_limit::digits>(133)<<endl; (iii) arithmetic operations=3x (vi) string bin_num; cout<<”Input a binary number as a string”<<endl; cin>>bin_num; cout << bin_num” as number: " << bitset<100>(bin_num).to_ulong() << endl; (b) (i) 16 bits in both integral and binary representation sum. (ii) string stral, stral1; int n; cout<<"enter a binary number to add"<<endl; cin>>stral; cin>>stral1; cout<<"enter a number of bits"<<endl; cin>>n; bitsetbitvec1(stral+stral1); cout<<bitvec1; (iii) 4n (c) (i) 16 bits in both

(ii) string strum, drum; int n; cout<<"enter a binary number to add"<<endl; cin>>strum; cin>>drum; cout<<"enter a number of bits"<<endl; cin>>n; bitsetbitvec1(strumdrum); cout<<bitvec1<<endl; (iii) 4n

  1. (a) 15 * 17 2327 35 7 34 11 54 17 246 3 68 5 108 8 492 1 136 2 216 4 984 255 1 432 2 1968 621 1 3936 4305 (b) The first number being multiplied has the same binary representation when halved, but having the last value dropped. Same with second number as well. Therefore, adding the doubles of the second numbers ommiting the number with an even number in the row yields the correct product. (c) Inp_1: decimal number; Inp_2: decimal number; Total=0; If( Inp_2<Inp_1){ Output: Inp_2Inp_ While(Inp_2 !=1){ Inp_2=Inp_2/ Inp_1=Inp_1 cout<<Inp_2<<” “<<Inp_1<<endl; If ((Inp_2/Inp_2)!=1) { Total=Total+Inp_1} } } cout<<Total<<endl;