我在玩代码制作精灵。我有一个朝右的精灵,并且一直在手动创建一个朝左的精灵。我正在寻找一种方法来反转十六进制0x以使子画面左移。子画面的一行是0x01555500,相反是0x05555400。有谁知道我该如何使用JavaScript做到这一点?

例:

数组:

[
    0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x00554000, 0x01555500, 0x02AFB000, 0x0BBFBF00,
    0x0BAFEFC0, 0x0AFFAA00, 0x00FFFC00, 0x029A8000,
    0x0A9A6A00, 0x2A956A80, 0x3E75DBC0, 0x3F555FC0,
    0x3D5557C0, 0x01505400, 0x0A802A00, 0x2A802A80
]


而相反的是:

[
    0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x00155000, 0x05555400, 0x00EFA800, 0x0FEFEE00,
    0x3FBFAE00, 0x0AAFFA00, 0x03FFF000, 0x002A6800,
    0x0A9A6A00, 0x2A956A80, 0x3E75DBC0, 0x3F555FC0,
    0x3D5557C0, 0x01505400, 0x0A802A00, 0x2A802A80
]

最佳答案

您可以使用此:

function reverse(line) {
    line2 = 0;
    for (var i = 0; i < 31; i++) {
        line2 = (line2 << 1) | (line & 1)
        line >>= 1;
    }
    return line2;
}




function reverse(line) {
    line2 = 0;
    for (var i = 0; i < 31; i++) {
        line2 = (line2 << 1) | (line & 1)
        line >>= 1;
    }
    return line2;
}
// demo:
var line = 0x01555500;
var line2 = reverse(line);
console.log(('0000000' + line.toString(16)).slice(-8));
console.log(('0000000' + line2.toString(16)).slice(-8));

08-16 20:57