### how can replace and add to array

 ``12345678910111213141516171819202122232425262728293031`` ``````#include using namespace std; // function to convert decimal to binary void decToBinary(int n) { // array to store binary number int binaryNum[32]; // counter for binary array int i = 0; while (n > 0) { // storing remainder in binary array binaryNum[i] = n % 2; n = n / 2; i++; } // printing binary array in reverse order for (int j = i - 1; j >= 0; j--) cout << binaryNum[j]; } // Driver program to test above function int main() { int n = 10000; decToBinary(n); return 0; }``````

Output:
10011100010000

I want change out put like this [ by code] :
110111000100010

Last edited on
Hawlong wrote:
Output:
10011100010000

I want change out put like this [ by code] :
110111000100010

Would you like to have another go at explaining what you want to do.
Last edited on
Well 10000 decimal is 10011100010000 as binary - so what's the issue?

Do you mean you want the output left filled with 0's like this:

 ``123456789101112131415161718192021`` ``````#include // function to convert decimal to binary void decToBinary(int n) { // array to store binary number int binaryNum[32] {}; // counter for binary array for (size_t i {}; n > 0; ++i, n /= 2) // storing remainder in binary array binaryNum[i] = n % 2; // printing binary array in reverse order for (size_t j = 32; j > 0; --j) std::cout << binaryNum[j - 1]; } // Driver program to test above function int main() { decToBinary(10000); }``````

 ``` 00000000000000000010011100010000 ```

This can also be done simply using std::bitset:

 ``123456789101112`` ``````#include #include // function to convert decimal to binary void decToBinary(unsigned n) { std::cout << std::bitset<32> {n} << '\n'; } // Driver program to test above function int main() { decToBinary(10000); }``````

See:
https://cplusplus.com/reference/bitset/bitset/
Last edited on
Thanks all for replay ,

I have a packet that comes encrypted from the server
to decode this packet , I convert it to binary and i change some bits ..

Example:
i have this bits : 10011100010000
i want add (1)bit 1001110(1)0010000 and change second bit to (1)
1(1)0111010010000

Thanks ..

Either use a std::string or a vector<bool>. Then you can corrupt your messages to your heart's content.
Well changing 100111 to 110111 is easy. Just change the appropriate element in the array. Adding an extra bit is best done with using a std::vector. Or use the std::bitset:

 ``12345678910111213141516171819202122`` ``````#include #include // function to convert decimal to binary void decToBinary(unsigned n) { constexpr size_t noBits { 16 }; constexpr size_t inspos { noBits - 8 }; static_assert (noBits >= 16 && noBits % 8 == 0); const auto s { std::bitset { n }.to_string() }; std::bitset bar { s.substr(1, inspos) + "1" + s.substr(inspos + 1) }; bar.set(13, 1); std::cout << bar << '\n'; } // Driver program to test above function int main() { decToBinary(10000); }``````

which displays:

 ``` 0110111010010000 ```

as required.
Last edited on
Thanks so much seeplus , solved
Topic archived. No new replies allowed.