我的练习是将多个数字分配给一个 long。1. 17 位数字 2. 7 位数字 3. 17 位数字 4. 7 位数字 5. 13 位数字 6. 3 位数字
一个函数将这 6 个整数打包成一个 long 第二个函数从一个 long 中读取这 6 个整数并打印信息
当我编译这个时,我得到了错误的数字。(像这样) Caller: 2012 Caller_zone: 92 Callee: 16398 Callee_zone: 123 Duration: 0 Tariff: 6
`
public class Problem_3_5{
public static void main(String[] args){
info(encode(130999, 101, 7777, 99, 7000, 6));
}
public static long encode(int caller, int caller_zone,
int callee, int callee_zone,
int duration, int tariff) {
long res = 0;
long example = 0;
//---1st---
example = caller;
res = res | example;
res = res << 17;
//---2nd---
example = caller_zone;
res = res | example;
res = res << 7;
//---3rd---
example = callee;
res = res | example;
res = res << 17;
//---4th---
example = callee_zone;
res = res | example;
res = res << 7;
//---5th---
example = duration;
res = res | example;
res = res << 13;
//---6th---
example = tariff;
res = res | example;
//--END---
return res;
//---------------------------
}
public static void info(long res){
//---TARIFF----
long tariff = 0;
tariff = (res & 7);
res = res >>> 3;
//---DURATION---
long duration = 0;
duration = (res & 8191);
res = res >>> 13;
//---CALLEE_ZONE---
long callee_zone = 0;
callee_zone = (res & 127);
res = res >>> 7;
//---CALLEE---
long callee = 0;
callee = (res & 131071);
res = res >>> 17;
//---CALLER_ZONE---
long caller_zone = 0;
caller_zone = (res & 127);
//---CALLER---
long caller = 0;
caller = (res & 131071);
繁华开满天机
慕无忌1623718
相关分类