C++ Binary sequence increment | SoloLearn: Learn to code for FREE!

+1

C++ Binary sequence increment

Hi everyone, I want to write a program that start from 0 and add 1 in each step for binary numbers in cpp I mean to have this output: 0,1,10,11,100,......

10/30/2020 9:31:54 AM

Armina

34 Answers

New Answer

+4

If 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.

+4

Armina ,what have you tried so far ?

+3

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.

+3

Martin Taylor Thank you so much🙏🏻🌷

+2

and write always your question with C++ ,example: “C++ Binary sequence increment ”

+2

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.

+2

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.

+1

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?

+1

i will try to help you, but i’m not strong in binary operations

+1

#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; }

+1

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?🙏🏻

+1

Martin Taylor Thanks alot for all of your explanations, I cant thank you enough🙏🏻🌷

0

создавай сдвиг на еденицу

0

<<< 1 or <<1 or >>1))

0

Sergey Can you please explain the whole process (algorithm or the code in c++) please?

0

sorry, no. i can’t to explain you

0

it’s need to read in google

0

or to watch code in visual studio, disassembly

0

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

0

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