43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import { _decorator } from 'cc'
|
|
|
|
const { ccclass, property } = _decorator
|
|
|
|
@ccclass('BitUtil')
|
|
export default class BitUtil {
|
|
// index是二进制从右到左
|
|
public static isBitSet(value: number, index: number): boolean {
|
|
const str: string = value.toString(2)
|
|
return Number.parseInt(str[str.length - 1 - index]) === 1
|
|
}
|
|
|
|
// 从右到左计算
|
|
public static setBitValue(value: number, index: number): number {
|
|
let newValue: number = value
|
|
const str: string = value.toString(2)
|
|
let newStr: string = ''
|
|
const maxIndex = Math.max(str.length - 1, index)
|
|
for (let i = 0; i <= maxIndex; i++) {
|
|
if (index === i) {
|
|
newStr = `1${newStr}`
|
|
} else {
|
|
if (str[i] === undefined) newStr = `0${newStr}`
|
|
else newStr = str[i] + newStr
|
|
}
|
|
}
|
|
newValue = Number.parseInt(newStr, 2)
|
|
return newValue
|
|
}
|
|
|
|
public static clearBitValue(value: number, index: number) {
|
|
let newValue: number = value
|
|
const str: string = value.toString(2)
|
|
let newStr: string = ''
|
|
for (let i = str.length - 1; i >= 0; i--) {
|
|
if (index === str.length - 1 - i) newStr = `0${newStr}`
|
|
else newStr = str[i] + newStr
|
|
}
|
|
newValue = Number.parseInt(newStr, 2)
|
|
return newValue
|
|
}
|
|
}
|