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;
}
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.
A leading zero is any 0 digit that comes before the first nonzero digit in a number's binary form.
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.
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.
[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).
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:
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).
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;
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";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With