Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Is not arithmetic shift better in countSetBits.js?

オープン
#1,152 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

@hitaishi-goel がすでに取り組んでいます。

2024年7月20日 から。

  • #1155 @hitaishi-goel による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
38/100
issue の種類
リファクタリング
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
javascript
領域
tooling

調査の方向性

Start in countSetBits.js at the shift on the line using >>>= 1. Read the surrounding function and any nearby tests or documentation to determine the intended behavior for negative numbers. Done means the shift behavior is explicitly settled and the implementation and relevant tests consistently reflect that decision.

索引モデルが issue の本文から書いたものです。

説明

The file countSetBits.js has the function:

/**
 * @param {number} originalNumber
 * @return {number}
 */
export default function countSetBits(originalNumber) {
  let setBitsCount = 0;
  let number = originalNumber;

  while (number) {
    // Add last bit of the number to the sum of set bits.
    setBitsCount += number & 1;

    // Shift number right by one bit to investigate other bits.
    number >>>= 1;
  }

  return setBitsCount;
}

I wonder if it wouldn't be better to use an arithmetic shift on line number >>>= 1 instead of a logical shift, thus preserving the sign of the number.

主要言語
JavaScript
スター
197k
フォーク
31k
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

trekhleb/javascript-algorithms のほかの issue

trekhleb/javascript-algorithms の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。