我想在一组 64 位上编码有关电话呼叫的数据。
有64 total: int caller(first 17 bits), int caller_zone(next 7 bits), int callee(17 bits), int callee_zone(7 bits), int duration(13 bits), int tariff(3 bits)
结束后,我想将其解码回来。我已经创建了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;
}