34 Answers

New AnswerIf you want to see a binary representation of an integer in C++ just use a bitset. https://www.cplusplus.com/reference/bitset/bitset/ Here is an example of using a bitset... https://code.sololearn.com/cdR3Apw37o5U/#cpp I've never understood why people use computationally expensive algorithms involving modulo arithmetic and powers of two when a simple bitmask or bitset will do the job.

I can't provide you with solution till the time I don't see any effort from your side. You must have written some kind of code or must have at least thought of doing something.

Armina, Here is an example that uses a 4-bit counter. https://code.sololearn.com/cwlIdqR3sG2v/#cpp I don't know why people keep overthinking binary conversion.

Armina The method used to convert an integer into a binary representation is going to vary from one language to another. This is mostly due to the way different languages store numbers. Java has the Integer.toBinaryString() method, and others for long etc. C++ has the bitset class. Python does not store integers in a fixed width format. JavaScript stores all numbers as double precision floating point numbers, following the international IEEE 754 standard. Some languages have better Boolean functions or string functions. It also makes a difference if you are converting signed or unsigned numbers. In your previous post at: https://www.sololearn.com/Discuss/2567883/decimal-to-binary-conversion-method-for-integers-numbers we discussed several generic methods employed in creating binary representations of numbers. They all have their strengths and weaknesses. The modulo arithmetic algorithms are more suitable for scripting languages which do not use fixed length integers while bitmasks are more appropriate, and faster, when using fixed length integers. If a particular language has a feature that makes your life easier then you should use it.

Arsenic I did write some algorithm and flowcharts but they are wrong and i dont know how to correct them. I want to use exclusive or to sum the number with 1 and define an array for each binary number. a=k=a[k+1] while(a%10>=0) { d=a%10 ^ 1 ^ a[k+1] if(d==1) a[k]=d else { a[k]=0 a[k+1]=1 } a= reverse a[k] } I know this code is wrong , can you elp me with the correct one?

#include <iostream> #include <bitset> #include <climits> using namespace std; int main() { int i = 0b000001; for(int j =0; j< 10; j++) std::cout << (std::bitset<sizeof(i <<= 1) * CHAR_BIT>(i <<= 1)) << "\n"; return 0; }

Martin Taylor Thanks alot, if generally i want to write the algorithm for showing these sequence, how should i write it? l mean the algorithm and flowchart that can be used in every language not the code for specific language? I did want to seprate each digit and with exclusive or operator to carry digit and 1 and the digit of number and store them in an array, but i had some problems with that . Is my idea right or it is totally wrong? Can you help to correct my algorithm please?🙏🏻

Arsenic I saw codes on some websites that calculate the sum of 2 n bit binary numbers, but i dont know know to write the code or even the algorithm for the binary increment

better write code and you give link on the your code, example so: C# It’s a dirty code. How to do clear code? Why the test 4th fail?Это грязный код. Как сделать чистый код? Почему 4-ый тест не проходит? https://code.sololearn.com/cVlh76Y5zg6n/?ref=app