Skip to main content
added 49 characters in body
Source Link
138 Aspen
  • 6.6k
  • 1
  • 4
  • 13
added 21 characters in body
Source Link
138 Aspen
  • 6.6k
  • 1
  • 4
  • 13

It's also worth noting that this function operates in constant time\$\color{blue}{\text{this function operates in constant time}}\$. That is, regardless of the input value, it always takes the same amount of time to execute. This is because the number of bits in an integer is fixed, so no matter the value, the same number of bits need to be examined.

[package]
name = "rust_hello"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
uint = "0.9"
# bitvec = "1.0.1"

It's also worth noting that this function operates in constant time. That is, regardless of the input value, it always takes the same amount of time to execute. This is because the number of bits in an integer is fixed, so no matter the value, the same number of bits need to be examined.

[package]
name = "rust_hello"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
uint = "0.9"
bitvec = "1.0.1"

It's also worth noting that \$\color{blue}{\text{this function operates in constant time}}\$. That is, regardless of the input value, it always takes the same amount of time to execute. This is because the number of bits in an integer is fixed, so no matter the value, the same number of bits need to be examined.

[package]
name = "rust_hello"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
uint = "0.9"
# bitvec = "1.0.1"
Source Link
138 Aspen
  • 6.6k
  • 1
  • 4
  • 13

Rust

Rust has its built-in popcount function: (num as u128).count_ones();

It's also worth noting that this function operates in constant time. That is, regardless of the input value, it always takes the same amount of time to execute. This is because the number of bits in an integer is fixed, so no matter the value, the same number of bits need to be examined.

u128

Try it online!

use std::time::Instant;

fn main() {
    let numbers = vec![5, 1254, 56465];
    let answers = vec![2, 6, 8];

    for (i, &num) in numbers.iter().enumerate() {
        let start = Instant::now();
        let result = (num as u128).count_ones();
        let duration = start.elapsed();

        assert_eq!(result, answers[i], "Failed test for {}", num);
        println!("Number: {}, Ones: {}, Time taken: {:?}", num, result, duration);
    }
}

u256

/src/main.rs

use uint::construct_uint;
use std::time::Instant;

construct_uint! {
    pub struct U256(4);
}

fn count_ones(n: U256) -> u32 {
    n.as_ref().iter().map(|&b| b.count_ones()).sum()
}

fn main() {
    let numbers = vec![
        U256::from(5u64),
        U256::from(1254u64),
        U256::from(56465u64),
        U256::from(18446744073709551615u64),
        U256::from_dec_str("115792089237316195423570985008687907853269984665640564039457584007913129639935").unwrap(),  // 2^256 - 1
    ];

    let answers = vec![2, 6, 8, 64, 256];

    for (i, num) in numbers.iter().enumerate() {
        let start = Instant::now();
        let result = count_ones(num.clone());
        let duration = start.elapsed();

        assert_eq!(result, answers[i], "Failed test for {}", num);
        println!("Number: {}, Ones: {}, Time taken: {:?}", num, result, duration);
    }
}

Cargo.toml

[package]
name = "rust_hello"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
uint = "0.9"
bitvec = "1.0.1"

Build and running

# win10 environment

$ cargo build
$ target\debug\rust_hello.exe


Number: 5, Ones: 2, Time taken: 600ns
Number: 1254, Ones: 6, Time taken: 700ns
Number: 56465, Ones: 8, Time taken: 400ns
Number: 18446744073709551615, Ones: 64, Time taken: 700ns
Number: 115792089237316195423570985008687907853269984665640564039457584007913129639935, Ones: 256, Time taken: 800ns