简体   繁体   English

如何修剪 32 位二进制数的所有前面的零?

[英]How to trim all the preceding zeroes of a 32-bit binary number?

I was trying to trim all the leading/preceding zeroes in a binary 32-bit number and print out the output by these following 2 codes of which the first one showed error while the other code was successful:我试图修剪二进制 32 位数字中的所有前导/前导零,并通过以下 2 个代码打印输出,其中第一个代码显示错误,而另一个代码成功:

I want to know why the former one failed.我想知道前一个失败的原因。 please explain in detail.请详细解释。 it would mean a lot to me.这对我来说意义重大。

    string trimmer(string& binary_str) {

    int i=0;// 'i' will finally have the index of the binary string which is having value as '0' and is 
            //  just the preceding zero to the first 1 in the string 

    while (binary_str.at(i) != 1) {//this will iterate over the srting until it reaches the zero before 
                                   //the first 1 in the string
        i++;
    }
    binary_str.erase(0, i);      //this will erase all the preceeding zeroes
    return binary_str;
}

This above written code didnt work and showed up error,上面写的代码不起作用并出现错误,

But this second code worked:但是这第二个代码有效:

string trimmer(string& binary_str) {
    int i=0;
    for (i = 0; i < 32; i++) {
        if (binary_str.at(i) == '1') {
            break;
        }
    }
    binary_str.erase(0, i);
    return binary_str;
}

Both had the function, ie, if input was given: a binary string 00000000000000000000001011001011 then the output should be 1011001011.两者都有函数,即,如果给出输入:一个二进制字符串 00000000000000000000001011001011 那么输出应该是 1011001011。

The former code used while loop whereas the latter used for loop with if conditional.前一个代码使用 while 循环,而后者使用 if 条件循环。 bot codes follow the same logic.机器人代码遵循相同的逻辑。 but the former one with while loop showed up error whereas the latter one didn't.但是前一个带有while循环的出现错误而后一个没有。

Can anybody explain to me the reason behind the while loop to show up error?任何人都可以向我解释 while 循环背后显示错误的原因吗? it would mean a lot to me.这对我来说意义重大。

The complete code is as follows:完整代码如下:

#include<bits/stdc++.h>
using namespace std;
/*
string trimmer(string& binary_str) {
    int i=0;
    while (binary_str.at(i) != 1) {
        i++;
    }
    binary_str.erase(0, i);
    return binary_str;
}
*/
string trimmer(string& binary_str) {
    int i=0;
    for (i = 0; i < 32; i++) {
        if (binary_str.at(i) == '1') {
            break;
        }
    }
    binary_str.erase(0, i);
    return binary_str;
}

int main(){
    string bStr="00000000001011001011"; 
    bStr = trimmer(bStr);
    cout<< bStr<<'\n';
    return 0;
}

You can use find_first_not_of to find the first character of that isn't a 0 .您可以使用find_first_not_of来查找不是0的第一个字符。 Then you can erase all characters up to that point.然后您可以erase到该点的所有字符。 If a string is all zeros, it will be completely erased.如果一个字符串全为零,它将被完全擦除。

void trim_leading_zeros(std::string &str) {
    str.erase(0, str.find_first_not_of('0'));
}

Try using modern C++ features:尝试使用现代 C++ 功能:

#include <iostream>
#include <algorithm>
#include <string>

void ltrim_zero(std::string& str) {
    str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](int character) {
        return '0' != character;
    }));
}

int main(){
    std::string bStr = "00000000001011001011"; 
    ltrim_zero(bStr);
    std::cout << bStr << std::endl;
    return 0;
}

Try live尝试直播

find the first occurrence of '1' and use this variation of string constructor找到第一次出现的 '1' 并使用这个字符串构造函数的变体

    string (const string& str, size_t pos, size_t len = npos);

str=string to be copied from str=要复制的字符串

pos= starting position to copy from pos=要复制的起始位置

npos= number of character to copy from source(str) string. npos= 要从源(str)字符串复制的字符数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用位集或浮点变量的情况下从 C++ 中的 32 位十六进制值表示二进制浮点数? - How to represent a floating point number in binary from 32-bit hex value in C++ without using bitset or float variable? 生成特定的32位二进制序列 - generating specific 32-bit binary sequence 32位机器如何计算双精度数 - How does a 32-bit machine compute a double precision number 从32位和64位二进制文​​件中读取 - Reading from a binary file on 32-bit and 64-bit 十进制数到32位二进制 - Decimal number to 32 bit binary 如何将用us表示时间的32位int转换为以秒表示的二进制分数的32位int表示时间? - How do I convert from a 32-bit int representing time in usec to a 32-bit int representing time as a binary fraction in secs? 查找 32 位数中第一个(最低)设置位的 position - Find position of first (lowest) set bit in 32-bit number 从C ++中的二进制文件读取32位整数? - Read 32-bit integer from binary file in C++? 该算法如何计算 32 位整数中设置的位数? - How does this algorithm to count the number of set bits in a 32-bit integer work? 如何在 C++ 中使用 integer 限制。数字“-91283472332”超出了 32 位有符号 integer 的范围 - How to work with integer limits in C++. The number "-91283472332" is out of the range of a 32-bit signed integer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM