将两个整数合并成一个,然后解码 [英] Combine two integers into one and decode them later

查看:97
本文介绍了将两个整数合并成一个,然后解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C ++我需要将两个不同的ID组合成一个16位整数。然后稍后我需要将这个16位整数解码为两个原始ID值。



示例:

  //将两个整数存储到一个
unsigned short Identifier1 = 12793; //(maximum number 30000)
unsigned short Identifier1 = 5450; //(maximum number 30000)
unsigned short CombinedIDs = 34283; //这是例子,我不知道的代码

//解码一个整数为两个
//此时我只有CombinedIDs值,我需要提取它
// into the two original IDs

unsigned short OriginalIdentifier1 = ... CombinedIDs ..获取代码12793
无符号short OriginalIdentifier2 = ... CombinedIDs ..获取代码5450


解决方案

这是不可能的。



假设你的两个标识符可以在 [0,30000] 的范围内,有30000 x 30000 =〜2 ^ 30可能的对身份标识。然而,只有2 ^ 16个可能的16位数字。因此,您不能将标识符对映射到16位整数,并希望从中恢复标识符。






您可以使用32位整数来存储组合,这种情况下编码和解码都很简单:

  unsigned short标识符1 = 12793; 
unsigned short Identifier2 = 5450;
unsigned int CombinedIDs =(Identifier1<< 16)|标识符2;

解码:

 code> unsigned short Identifier1 = CombinedIDs>> 16; 
unsigned short Identifier2 = CombinedIDs& 0x0000FFFF

请注意,现在不需要限制标识符在[0,30000]

回答您的意见:4和12位是可能的。



编码:

  unsigned short Identifier1; // 4 bits 
unsigned short Identifier2; // 12 bits
unsigned short CombinedIDs =(Identifier1 << 12)|标识符2;

解码:

 code> unsigned short Identifier1 = CombinedIDs>> 12; 
unsigned short Identifier2 = CombinedIDs& 0x0FFF;


Using C++ I need to combine two distinct IDs into one 16bit integer. Then later on I need to decode this 16bit integer into the two original ID values.

Example:

// Store two integers into one
unsigned short Identifier1 = 12793; //(maximum number 30000)
unsigned short Identifier1 = 5450; //(maximum number 30000)
unsigned short CombinedIDs = 34283; // this is example, I don't know the code for that

// Decode one integer into two
// At this point I only have CombinedIDs value, I need to extract it
// into the two original IDs

unsigned short OriginalIdentifier1 = ...CombinedIDs.. code to get 12793
unsigned short OriginalIdentifier2 = ...CombinedIDs.. code to get 5450

解决方案

This is impossible.

Assuming your two identifiers can be in the range [0, 30000], there are 30000 x 30000 = ~2^30 possible pairs of identifiers. However, there are only 2^16 possible 16-bit numbers. So, you can't possibly map pairs of identifiers to a 16-bit integer and expect to recover the identifiers from it.


Instead, you can use a 32-bit integer to store the combination, in which case both encoding and decoding is straightforward:

Encoding:

unsigned short Identifier1 = 12793;
unsigned short Identifier2 = 5450;
unsigned int CombinedIDs = (Identifier1 << 16) | Identifier2;

Decoding:

unsigned short Identifier1 = CombinedIDs >> 16;
unsigned short Identifier2 = CombinedIDs & 0x0000FFFF

Note that now the restriction that the identifiers be in the range [0, 30000] is not necessary - they be any unsigned short value.


EDIT Answering your comment: 4 and 12 bits are possible.

Encoding:

unsigned short Identifier1;  // 4 bits
unsigned short Identifier2;  // 12 bits
unsigned short CombinedIDs = (Identifier1 << 12) | Identifier2;

Decoding:

unsigned short Identifier1 = CombinedIDs >> 12;
unsigned short Identifier2 = CombinedIDs & 0x0FFF;

这篇关于将两个整数合并成一个,然后解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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