1/*
2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16export class BitopsBitsInByte {
17  private static bitsinbyte(b: int): int {
18    let m: int = 1;
19    let c: int = 0;
20    while (m < 0x100) {
21      if ((b & m) != 0) {
22        c++;
23      }
24      m <<= 1;
25    }
26    return c;
27  }
28
29  private n1: int = 350;
30  private n2: int = 256;
31  private static readonly expected: int = 358400;
32
33  public run(): void {
34    let sum: int = 0;
35    for (let x: int = 0; x < this.n1; x++) {
36      for (let y: int = 0; y < this.n2; y++) {
37        sum += BitopsBitsInByte.bitsinbyte(y);
38      }
39    }
40    assert sum == BitopsBitsInByte.expected: "Incorrect result";
41  }
42}
43
44function main(): void {
45  let a = new BitopsBitsInByte;
46  a.run();
47}
48
49
50