プログラミングのお題スレ Part22 (857レス)
上下前次1-新
抽出解除 必死チェッカー(本家) (べ) 自ID レス栞 あぼーん
リロード規制です。10分ほどで解除するので、他のブラウザへ避難してください。
659: デフォルトの名無しさん [sage] 2025/02/27(木) 08:26:19.30 ID:LSRTW28H(1) AAS
>>656656(5): デフォルトの名無しさん [] 2025/02/26(水) 21:33:47.93 ID:rkiIsmEI(1) AAS
お題: Python の int.bit_count()
65535 → 16
15 → 4
6 → 2
1 → 1
0 → 0
-1 → 1
-6 → 2
-15 → 4
-65535 → 16
Rust
trait BitCount {
fn bit_count(&self) -> usize;
}
impl BitCount for i32 {
fn bit_count(&self) -> usize {
self.unsigned_abs().count_ones() as usize
}
}
use num::{BigInt, One};
impl BitCount for BigInt {
fn bit_count(&self) -> usize {
self.iter_u64_digits().map(|x| x.count_ones() as usize).sum()
}
}
fn main() {
for (input, output) in [(65535, 16), (15, 4), (6, 2), (1, 1), (0, 0), (-1, 1), (-6, 2), (-15, 4), (-65535, 16)] {
assert_eq!(input.bit_count(), output);
}
assert_eq!(BigInt::from(-1).bit_count(), 1);
assert_eq!((BigInt::from(2).pow(1_000_000_000) - BigInt::one()).bit_count(), 1_000_000_000);
}
上下前次1-新書関写板覧索設栞歴
スレ情報 赤レス抽出 画像レス抽出 歴の未読スレ AAサムネイル
ぬこの手 ぬこTOP 0.037s