Adding two binary arrays in C++ -
how can add 2 binary arrays(8 bits) , store result in third binary array in visual studio 2010 - c++ . example : a=00100011 b=11011100 result_array=11111111
i tried doing using convert b (have decimal value) binary: std::string binary2 = std::bitset<8>(b).to_string();
but when added 2 binary array , storing them third integer array ,the third array didn't take value results adding operation , still having default value (which 00000000)
thanks in advance!
call binary arrays boolean arrays , work back:
#include <iostream> int main(int argc, char* argv[]) { bool a[8]={0,0,1,0,0,0,1,1}; bool b[8]={1,1,0,1,1,1,0,0}; bool c[8]; bool remainder=false; for(int x=7;x>=0;x--) { unsigned sum=0; if(remainder)++sum; if(a[x])++sum; if(b[x])++sum; if(sum%2==1)c[x]=true; else c[x]=false; if(sum>1)remainder=true; else remainder=false; } for(unsigned x=0;x<8;++x) { std::cout << c[x]; if(x!=7)std::cout << ","; } std::cout << std::endl; return 0; }
Comments
Post a Comment