C++ 使用逐位运算符将少量整数合并为无符号长(64位)
我想将有关电话呼叫的数据编码为一组64位 总共有64位:int调用者(前17位)、int调用者区域(后7位)、int被调用者区域(17位)、int被调用者区域(7位)、int持续时间(13位)、int费率(3位) 捐赠后,我想解码回来。我已经创建了编码数据的C++ 使用逐位运算符将少量整数合并为无符号长(64位),c++,encoding,bit-shift,binary-operators,C++,Encoding,Bit Shift,Binary Operators,我想将有关电话呼叫的数据编码为一组64位 总共有64位:int调用者(前17位)、int调用者区域(后7位)、int被调用者区域(17位)、int被调用者区域(7位)、int持续时间(13位)、int费率(3位) 捐赠后,我想解码回来。我已经创建了编码数据的encode(…)方法,但我认为这是错误的(因为在解码调用者返回后,号码是不同的) 问题:如何纠正编码方法以及如何解码数据 代码编译并运行(如果运行,很容易理解): #include "stdafx.h" #include <iostr
encode(…)
方法,但我认为这是错误的(因为在解码调用者返回后,号码是不同的)
问题:如何纠正编码方法以及如何解码数据
代码编译并运行(如果运行,很容易理解):
#include "stdafx.h"
#include <iostream>
using namespace std;
unsigned long long encode(int caller, int caller_zone, int callee, int callee_zone, int duration, int tariff){
//I will shift every number to the left an ammount of times so they will cover seperate area of 64 bits of unsigned long long
unsigned long long p1 = caller;
int shift = 64 - 17;//first 17 bits
p1 = __ll_lshift(p1, shift);
unsigned long long p2 = caller_zone;
p2 = __ll_lshift(p2, (shift -= 7));//next 7 bits
unsigned long long p3 = callee;
p3 = __ll_lshift(p3, (shift -= 17));//next 17 bits
unsigned long long p4 = callee_zone;
p4 = __ll_lshift(p4, (shift -= 7));//next 7 bits
unsigned long long p5 = duration;
p5 = __ll_lshift(p5, (shift -= 13));//next 13 bits
unsigned long long p6 = tariff;//last 3 bits
return p1 | p2 | p3 | p4 | p5 | p6;
}
void info(long long val){
unsigned long long val1 = 0;
//
int caller; int caller_zone;
int callee; int callee_zone;
int duration; int tariff;
caller = __ll_lshift(ULLONG_MAX & val, 64 - 17);
cout << "caller: " << caller << endl;
}
int main(){
int caller = 130999; int caller_zone = 101;
int callee = 7777; int callee_zone = 99;
int duration = 7000; int tariff = 6;
cout << "FROM MAIN" << endl;
cout << "caller: " << caller << endl
<< "caller zone: " << caller_zone << endl
<< "calee: " << callee << endl
<< "calee zone: " << callee_zone << endl
<< "duration: " << duration << endl
<< "tariff: " << tariff << endl;
unsigned long long u = encode(caller, caller_zone, callee, callee_zone, duration, tariff);
cout << u << endl;// do skasowania
cout << "\n FROM INFO" << endl;
info(u);
cout << "cos" << endl;
int val = 21;
val = __ll_lshift(val, 1);
cout << val << endl;
system("pause");
return 0;
}
#包括“stdafx.h”
#包括
使用名称空间std;
无符号长-长编码(int调用者、int调用者\区域、int被调用者、int被调用者\区域、int持续时间、int费率){
//我将把每个数字向左移动一次,这样它们将覆盖64位无符号长字符的单独区域
无符号长p1=调用者;
int shift=64-17;//前17位
p1=换档(p1,换档);
无符号长p2=呼叫方\区域;
p2=uu ll_ulshift(p2,(shift-=7));//下一个7位
无符号长p3=被调用方;
p3=uu ll_ulshift(p3,(shift-=17));//下一个17位
无符号长p4=被调用方区域;
p4=uu ll_ulshift(p4,(shift-=7));//下一个7位
无符号长p5=持续时间;
p5=u ll_lshift(p5,(shift-=13));//下一个13位
无符号长p6=关税;//最后3位
返回p1 | p2 | p3 | p4 | p5 | p6;
}
无效信息(长val){
无符号长值1=0;
//
int调用者;int调用者_区域;
int callee;int callee_区域;
国际关税;
呼叫者=uu ll_lshift(ULLONG_MAX&val,64-17);
cout似乎您正在按照您的意愿进行编码,尽管您必须亲自查看以下程序的输出才能确定
我从中偷了一个函数,并对您的代码进行了一些修改,使其更易于移植
#include <iostream>
#include <stdint.h>
#include <limits.h>
#include <stdio.h>
using namespace std;
//assumes little endian
void printBits(size_t const size, void const * const ptr)
{
unsigned char *b = (unsigned char*) ptr;
unsigned char byte;
int i, j;
for (i=size-1;i>=0;i--)
{
for (j=7;j>=0;j--)
{
byte = b[i] & (1<<j);
byte >>= j;
printf("%u", byte);
}
}
puts("");
}
uint64_t encode(int caller, int caller_zone, int callee, int callee_zone, int duration, int tariff){
uint64_t p1 = caller;
int shift = 64 - 17;//first 17 bits
p1 = p1 << shift;
uint64_t p2 = caller_zone;
p2 = p2 << (shift -= 7); //next 7 bits
uint64_t p3 = callee;
p3 = p3 << (shift -= 17); //next 17 bits
uint64_t p4 = callee_zone;
p4 = p4 << (shift -= 7); //next 7 bits
uint64_t p5 = duration;
p5 = p5 << (shift -= 13);//next 13 bits
uint64_t p6 = tariff;//last 3 bits
printBits(8, &p1);
printBits(8, &p2);
printBits(8, &p3);
printBits(8, &p4);
printBits(8, &p5);
printBits(8, &p6);
uint64_t result = p1 | p2 | p3 | p4 | p5 | p6;
printBits(8, &result);
return result;
}
int main(){
int caller = 130999; int caller_zone = 101;
int callee = 7777; int callee_zone = 99;
int duration = 7000; int tariff = 6;
unsigned long long u = encode(caller, caller_zone, callee, callee_zone, duration, tariff);
return 0;
}
这是解码功能:
#include <iostream>
#include <tuple>
using namespace std;
std::tuple<int, int, int, int, int, int> decode(uint64_t bits) {
int tariff = bits & 0x7;
bits >>= 3;
int duration = bits & 0x1fff;
bits >>= 13;
int callee_zone = bits & 0x7f;
bits >>= 7;
int callee = bits & 0x1ffff;
bits >>= 17;
int caller_zone = bits & 0x7f;
bits >>= 7;
int caller = bits & 0x1ffff;
return std::make_tuple(caller, caller_zone, callee, callee_zone, duration, tariff);
}
int main()
{
uint64_t encodedBits = 0b1111111111011011111001010000111100110000111000111101101011000110;
int caller = 0; int caller_zone = 0;
int callee = 0; int callee_zone = 0;
int duration = 0; int tariff = 0;
std::tie(caller, caller_zone, callee, callee_zone, duration, tariff) = decode(encodedBits);
cout << caller << endl
<< caller_zone << endl
<< callee << endl
<< callee_zone << endl
<< duration << endl
<< tariff << endl;
return 0;
}
#包括
#包括
使用名称空间std;
std::元组解码(uint64\u t位){
int关税=位&0x7;
位>>=3;
int duration=位&0x1fff;
位>>=13;
int callee_zone=位&0x7f;
位>>=7;
int被调用方=位&0x1fff;
位>>=17;
int caller_zone=位&0x7f;
位>>=7;
int调用者=位&0x1fff;
return std::make_tuple(呼叫者、呼叫者区域、被呼叫者、被呼叫者区域、持续时间、费率);
}
int main()
{
uint64_t encodedBits=0B111111111101101111110010100001110011000011100011111011101011000110;
int caller=0;int caller\u zone=0;
int callee=0;int callee_zone=0;
国际持续时间=0;国际关税=0;
std::tie(呼叫者、呼叫者区域、被呼叫者、被呼叫者区域、持续时间、费率)=解码(编码比特);
不能确定编码数据的全部长度吗?我不知道你要什么…@πάνταῥεῖ 我该如何改进我的问题?“我该如何改进我的问题?”很难说,我真的一点线索都没有!(所有那些变化的东西:-/…)@πάνταῥεῖ 你们明白这个问题是关于什么的吗?我有几个数字,我想把它们合并成一个64位数字。64位数字表示依赖于机器体系结构,这就是为什么我在这里提出endianess问题!(对于编码/解码上下文)。我刚刚假设您可能希望通过一根电线发送编码数据并将其解码回来。否则,简单的位掩蔽和向下(右)移位以进行规范化应该可以让您很好地进行解码。
#include <iostream>
#include <tuple>
using namespace std;
std::tuple<int, int, int, int, int, int> decode(uint64_t bits) {
int tariff = bits & 0x7;
bits >>= 3;
int duration = bits & 0x1fff;
bits >>= 13;
int callee_zone = bits & 0x7f;
bits >>= 7;
int callee = bits & 0x1ffff;
bits >>= 17;
int caller_zone = bits & 0x7f;
bits >>= 7;
int caller = bits & 0x1ffff;
return std::make_tuple(caller, caller_zone, callee, callee_zone, duration, tariff);
}
int main()
{
uint64_t encodedBits = 0b1111111111011011111001010000111100110000111000111101101011000110;
int caller = 0; int caller_zone = 0;
int callee = 0; int callee_zone = 0;
int duration = 0; int tariff = 0;
std::tie(caller, caller_zone, callee, callee_zone, duration, tariff) = decode(encodedBits);
cout << caller << endl
<< caller_zone << endl
<< callee << endl
<< callee_zone << endl
<< duration << endl
<< tariff << endl;
return 0;
}