Overflow while attempting to calculate the Hamming weight in C++

I’m trying to write a short code that calculates the Hamming weight of integers:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Solution {
public:
int hammingWeight(int n) {
if(n==0){
return 0;
}else{
int a=1;
while(a<=(float)n/2){
a*=2;
}
return 1+hammingWeight(n-a);
}
}
};
</code>
<code>class Solution { public: int hammingWeight(int n) { if(n==0){ return 0; }else{ int a=1; while(a<=(float)n/2){ a*=2; } return 1+hammingWeight(n-a); } } }; </code>
class Solution {
public:
    int hammingWeight(int n) {
        if(n==0){
            return 0;
        }else{
            int a=1;
            while(a<=(float)n/2){
                a*=2;
            }
            return 1+hammingWeight(n-a);
        }
        
    }
};

However it gives error for n=2147483645:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Line 9: Char 18: runtime error: signed integer overflow: 1073741824 * 2 cannot be represented in type 'int' (solution.cpp)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior solution.cpp:9:18
</code>
<code>Line 9: Char 18: runtime error: signed integer overflow: 1073741824 * 2 cannot be represented in type 'int' (solution.cpp) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior solution.cpp:9:18 </code>
Line 9: Char 18: runtime error: signed integer overflow: 1073741824 * 2 cannot be represented in type 'int' (solution.cpp)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior solution.cpp:9:18

I don’t understand how, I never need to do 1073741824 * 2 in my calculation. Also my code works if instead of a<=(float)n/2, I just do a<=n/2.

19

The difference between a<=(float)n/2 and a<=n/2 is that in the former, a is converted to float to be compared with the float expression (float)n/2.

During this conversion some precision is lost because the 32 bit float representation does not have enough bits to represent 2147483645 accurately.
In this case the float value becomes 2147483648 (the closest value that can be represented in a float) which changes the comparisson outcome.

You can observe this using this minimal example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include<iostream>
int main() {
int n = 2147483645;
float f = n;
std::cout << std::fixed << f;
}
</code>
<code>#include<iostream> int main() { int n = 2147483645; float f = n; std::cout << std::fixed << f; } </code>
#include<iostream>

int main() {
    int n = 2147483645;
    float f = n;
    std::cout << std::fixed << f;
}

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>2147483648.000000
</code>
<code>2147483648.000000 </code>
2147483648.000000

Live demo

A side note:
A 64 bit double does have enough bits to represent 2147483645, so if you change the cast to double you should get the same result as without the cast (see minimal demo).

You can see some more info about limitations of floating point repesentations here: Which is the first integer that an IEEE 754 float is incapable of representing exactly?.

You can compare the number of representable binary digits for numeric types:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <limits>
constexpr std::numeric_limits<float> float_meta;
constexpr std::numeric_limits<int> int_meta;
std::cout<< "integer binary digits: " << int_meta.digits;
std::cout<< "float mantissa digits: " << float_meta.digits;
</code>
<code>#include <limits> constexpr std::numeric_limits<float> float_meta; constexpr std::numeric_limits<int> int_meta; std::cout<< "integer binary digits: " << int_meta.digits; std::cout<< "float mantissa digits: " << float_meta.digits; </code>
#include <limits>

constexpr std::numeric_limits<float> float_meta;
constexpr std::numeric_limits<int> int_meta;

std::cout<< "integer binary digits: " << int_meta.digits;
std::cout<< "float mantissa digits: " << float_meta.digits;

On a typical platform, the values might be 31 and 24. This means that 1<<30 is well beyond the precision of float type.

The expression a<=(float)n/2 promotes(converts) all the integer values (including a) to float, before calculating the result. If the value of a is larger than (1<<24)-1, the conversion to float looses precision, which is considered UB.

Your compile environment is running a sanitizer prior to compiling the code and it captures a problem before even trying to compile the code. Mind the term UndefinedBehaviorSanitizer in the output. Your code failed before even being compiled. This also signals you that the compile environment doesn’t tolerate any UB (which is usually ignored by the compiler in home/personal setups). So you have to learn C++ basics (UB included), before trying this challenge.
You can avoid this particular warning/error by validating your number before conversion:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <bit>
unsigned int a = 1;
while((std::bit_width(a) < float_meta.digits)
&& (a<=(float)n/2))
{/**/};
</code>
<code>#include <bit> unsigned int a = 1; while((std::bit_width(a) < float_meta.digits) && (a<=(float)n/2)) {/**/}; </code>
#include <bit>

unsigned int a = 1;
while((std::bit_width(a) < float_meta.digits)
   && (a<=(float)n/2))
{/**/};

or just avoid the cause by not converting to float:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <bit>
unsigned hammingWeight(unsigned n) {
return std::popcount(n);
};
</code>
<code>#include <bit> unsigned hammingWeight(unsigned n) { return std::popcount(n); }; </code>
#include <bit>
unsigned hammingWeight(unsigned n) {
    return std::popcount(n);
};

This code only counts the number of 1 bits in an unsigned number. Depending on the platform, std functions in <bit> may use compiler intrinsics that boil the calculation cost down to a single opcode. If the input is supposed to be signed then std::popcount(std::bit_cast<unsigned>(n)) gives the result for full range of signed, with No UB.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật