Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove leading zeroes from Binary converted from Decimal

I am solving a problem where I have to convert given first N natural numbers to binary numbers. I am using bitset and .to_string(). But, after the number is converted to binary it has some leading zeroes obviously as equal to the size of the given bitset. The task is to remove that. I have done that using std::string:: erase() But I think it's not a good approach to do so. How can I optimize this part of the code?

#include <iostream>
#include <bitset>
#include <string>
int main()
{
    int T;
    std:: cin >> T;
    while(T--) {
    int n;
    std:: cin >> n;
    for(auto i = 1; i <= n; ++i) {
        std::string binary = std::bitset<32>(i).to_string(); //to binary

        //This part here

        int j = 0;
        while(binary[j] == '0') {
            ++j;
        }
        binary.erase(0, j);

        //Till here

        std::cout<<binary<<" ";
    }
    std:: cout << std:: endl;
    }
    return 0;
}
like image 301
Arun Suryan Avatar asked Apr 13 '20 06:04

Arun Suryan


People also ask

How do you remove leading zeros from a binary number?

Use the inbuilt replaceAll() method of the String class which accepts two parameters, a Regular Expression, and a Replacement String. To remove the leading zeros, pass a Regex as the first parameter and empty string as the second parameter.

What is leading zeros in binary?

A leading zero is any 0 digit that comes before the first nonzero digit in a number's binary form.

How do you find the number of leading zeros?

Leading zero's in a binary number is equal to zeros preceding the highest order set bit of the number. 2. int lim = sizeof(int) * 8; is used to find the total number of bits required to store an integer in memory.

How to remove the leading zeros from numbers?

Select the numbers from which you want to remove the leading zeros. You will notice that there is a yellow icon at the top right part of the selection. That’s it! The above steps would remove the apostrophe and convert these text values back into numbers.

How to strip the zeros from the left side of decimal?

[1] # Strip the zeros from the left side of your split decimal out = out.lstrip('0') If you want the final result to be an integer or a float, simply convert it after using int (out) or float (out).

How to remove zeros at the beginning of a string in Python?

This time, the zeros at the beginning of our character strings have been kept, but the zeros at the end of the character strings were deleted. We can also use the str_remove function of the stringr package to remove trailing zeros:

How to count the leading zeros of an integer using GCC?

Solution 3: Using the GCC __builtin_clz (x): This function is used to count the leading zeros of the integer where clz stands for count leading zero’s. It counts a number of zeros before the first occurrence of one (set bit).


Video Answer


2 Answers

You could make use of the std::string::find_first_not_of() function to get the position of the first character that isn't a zero. Then use std::string::erase() to erase from the beginning of the string (index 0) to the position of the first non-zero character. This will avoid the while loop you're currently using.

Example:

std::string binary = std::bitset<32>(128).to_string(); //"00000000000000000000000010000000"
binary.erase(0, binary.find_first_not_of('0')); //"10000000"
std::cout << binary;
like image 87
jignatius Avatar answered Oct 28 '22 01:10

jignatius


I would suggest to use log2 function from cmath header file. You can count the number of bits you would require to represent the integer in binary format with this. Thus you won't need the while loop used to count the number of leading zeroes.

Here is the code:

#include <iostream>
#include <bitset>
#include <string>
#include <cmath>
int main()
{
    int T;
    std:: cin >> T;
    while(T--) {
    int n;
    std:: cin >> n;
    for(auto i = 1; i <= n; ++i) {
        std::string binary = std::bitset<32>(i).to_string(); //to binary
        int len = log2(i)+1;
        binary.erase(0,32-len);
        std::cout<<binary<<"\n";
    }
    std:: cout << std:: endl;
    }
    return 0;
}

As john mentioned in the comment section, you not necessarily need to remove the number of leading zeroes. For that you can do this,

std::cout<<binary.substr(32-len,len)<<"\n";
like image 28
ChasedByDeath Avatar answered Oct 28 '22 01:10

ChasedByDeath