从 2 进制整数读取和设置 3 进制数字

发布于 2024-11-10 05:48:10 字数 464 浏览 3 评论 0原文

我的应用程序数据的一部分包含一组 9 个三元(基数 3)“位”。为了保持数据库的数据紧凑,我想将该数据存储为单个短数据。由于 3^9 < 2^15 我可以将任何可能的 9 位 3 进制数字表示为短整型。

我当前的方法是将其作为长度为 9 的字符串来使用。我可以通过索引读取或设置任何数字,这很好而且很容易。为了将其转换为短整型,我目前正在手动转换为基数 10(使用移位加循环),然后使用 Int16.Parse 将其转换回二进制短整型。为了将存储的值转换回以 3 为基数的字符串,我以相反的方式运行该过程。所有这些都需要时间,如果可能的话我想对其进行优化。

我想做的就是始终将值存储为短值,然后读取并设置三进制位。理想情况下,我将拥有从二进制文件中获取和设置各个数字的函数。

我尝试过使用一些位移和 mod 函数,但还没有完全想出正确的方法来做到这一点。我什至不确定如果不进行完全转换是否可能。

谁能给我任何可以帮助解决这个问题的按位算术魔法?

Part of my application data contains a set of 9 ternary (base-3) "bits". To keep the data compact for the database, I would like to store that data as a single short. Since 3^9 < 2^15 I can represent any possible 9 digit base-3 number as a short.

My current method is to work with it as a string of length 9. I can read or set any digit by index, and it is nice and easy. To convert it to a short though, I am currently converting to base 10 by hand (using a shift-add loop) and then using Int16.Parse to convert it back to a binary short. To convert a stored value back to the base 3 string, I run the process in reverse. All of this takes time, and I would like to optimize it if at all possible.

What I would like to do is always store the value as a short, and read and set ternary bits in place. Ideally, I would have functions to get and set individual digits from the binary in place.

I have tried playing with some bit shifts and mod functions, but havn't quite come up with the right way to do this. I'm not even sure if it is even possible without going through the full conversion.

Can anyone give me any bitwise arithmetic magic that can help out with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋风の叶未落 2024-11-17 05:48:10
public class Base3Handler
{
    private static int[] idx = {1, 3, 9, 27, 81, 243, 729, 729*3, 729*9, 729*81};

    public static byte ReadBase3Bit(short n, byte position)
    {
        if ((position > 8) || (position < 0))
            throw new Exception("Out of range...");
        return (byte)((n%idx[position + 1])/idx[position]);
    }

    public static short WriteBase3Bit(short n, byte position, byte newBit)
    {
        byte oldBit = ReadBase3Bit(n, position);
        return (short) (n + (newBit - oldBit)*idx[position]);
    }
}
public class Base3Handler
{
    private static int[] idx = {1, 3, 9, 27, 81, 243, 729, 729*3, 729*9, 729*81};

    public static byte ReadBase3Bit(short n, byte position)
    {
        if ((position > 8) || (position < 0))
            throw new Exception("Out of range...");
        return (byte)((n%idx[position + 1])/idx[position]);
    }

    public static short WriteBase3Bit(short n, byte position, byte newBit)
    {
        byte oldBit = ReadBase3Bit(n, position);
        return (short) (n + (newBit - oldBit)*idx[position]);
    }
}
肤浅与狂妄 2024-11-17 05:48:10

这些都是小数字。根据需要将它们有效地存储在内存中,然后使用表查找根据需要从一种形式转换为另一种形式。

These are small numbers. Store them as you wish, efficiently in memory, but then use a table lookup to convert from one form to another as needed.

阳光下慵懒的猫 2024-11-17 05:48:10

您不能对三进制值进行位运算。您需要使用乘法、除法和取模来提取和组合值。

要使用位操作,您需要将打包限制为每个短接 8 个三进制(即每个 2 位)

You can't do bit operations on ternary values. You need to use multiply, divide and modulo to extract and combine values.

To use bit operations you need to limit the packing to 8 ternaries per short (i.e. 2 bits each)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文