Why does `count_ones` return a `u32`?

I'm curious if anyone knows why the various bitwise operations all take u32 for the bit units? It seems like u8 or u16 would work fine.

It's probably because the results of those operations are often used as shift distances, and the input argument for shift distance to the shift operators is a u32. Remember that Rust is designed to work with non-primitive numeric types, such as BigInt and BigUint, where small shift distance ranges would be insufficient.

1 Like

This is not true. The shift operators are implemented for every combination of integer types, and bigints can implement the shift operators for any number of integer types they wish.

2 Likes

@alice is correct; see this part of the rustc source.

1 Like