将2个4位数字存储在1个8位数字中 [英] Store 2 4-bit numbers in 1 8 bit number

查看:125
本文介绍了将2个4位数字存储在1个8位数字中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在考虑二进制数字方面是新手。我想知道是否可以将2个4位数字(即十六进制编码的数字)编码为1个8位数字。因此,如果我用 a 5 作为十六进制数字,那将是10和5。也许有办法将其存储在1个8位数字中,以这种方式,您可以将其从8位数字中提取回其组成的4位部分。

I am new in thinking about binary numbers. I'm wondering if there is a way to encode 2 4-bit numbers (i.e. hex-encoded numbers) into 1 8-bit number. So if I had a and 5 as the hex numbers, that would be 10 and 5. Maybe there is a way to store that in 1 8 bit number, in such a way that you can get it out of the 8-bit number back into its component 4-bit parts.

[10, 5]! = 15
15! = [10, 5]

很想知道是否有一种编码数字的方法来实现这一点。

Wondering if there is such a way to encode the numbers to accomplish this.

这似乎是可能的,因为第一个值可以存储在前16个数字中,然后下一个值可以存储在剩余的中,使用16作为1 ,32 as 2,48 as 3,依此类推。

It seems like it is possible, because the first value could be stored in the first 16 digits, then the next value could be stored in the remaining, using 16 as 1, 32 as 2, 48 as 3, etc.

无法确定此处的答案是怎么做的:

Can't tell if the answer here is how to do it:

如何我可以在一个1字节的字符中存储2个数字吗?

没有真正给出我想要的东西:

Not really giving what I'd want:

> a = 10
10
> b = 5
5
> c = a + b
15
> d = (c & 0xF0) >> 4
0
> e = c & 0x0F
15

也许我使用的方式不正确,不确定。似乎也可以,但是我不太确定如何在JavaScript中完成此操作。

Maybe I'm not using it right, not sure. This seems like it could be it too but I am not quite sure how to accomplish this in JavaScript.

如何将2个4位无符号数字合并为1个8位数字C

任何帮助将不胜感激。谢谢!

Any help would be greatly appreciated. Thank you!

推荐答案

我认为第一篇文章都有密钥。

I think the first post has the key.

具有 a 5 作为要存储的两个4位十六进制数字,您可以将它们存储在变量中像这样:

Having a and 5 as the two 4-bit hex numbers to store, you can store them in a variable like:

var store = 0xa5;

或动态

var store = parseInt('0x' + ('a' + '9'), 16);

然后提取零件:

var number1 = ((store & 0xF0) >> 4).toString(16)
var number2 = ((store & 0x0F)).toString(16)

我希望这会有所帮助。

这篇关于将2个4位数字存储在1个8位数字中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆