3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【python3】10進数を2進数に変換したときの1の数を求める

Last updated at Posted at 2025-08-11

はじめに

0進数を2進数に変換し、1の個数を数えるプログラムで、bit_countメソッドを使うと一瞬でできるというのをコメントで教えていただいたので、記事に致します

bit_countメソッドとは?

Python 3.10から実装されたメソッドで、整数intの10進数を2進数に変換したときの1の個数を数えるメソッドである。

実装例

以下のように使います。

# int型の整数を入力する
n = int(input())
# 1の個数を数える
ans = n.bit_count()
# 出力する
print(ans)

実行結果

実行結果は以下の通りである。

test1.jpg

最後に

便利な機能があるのですね。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?