data[1]: 00010 000
data[2]: 00101 100
需要放入一个新的字节中
d: 000 00101
谢谢!
22 个解决方案
#1
union{
struct {
unsigned char x;
unsigned char y;
}a;
unsigned short b;
}temp;
int main(){
temp.a.x=16; //00010 000
temp.a.y=44; //00101 100
temp.b >>= 3;
cout << (int)temp.a.y << endl; //输出5 //000 00101
return 0;
}
struct {
unsigned char x;
unsigned char y;
}a;
unsigned short b;
}temp;
int main(){
temp.a.x=16; //00010 000
temp.a.y=44; //00101 100
temp.b >>= 3;
cout << (int)temp.a.y << endl; //输出5 //000 00101
return 0;
}
#2
楼长快揭帖呀.求求你了.
#3
呵呵,楼上的把union玩的出神入化,小弟开眼了。
但是,如果要求将 第一个字节的奇数位 和 第二个字节的偶数位 合并成一个字节,你这个方法就不通用了吧,要怎么做呢???
但是,如果要求将 第一个字节的奇数位 和 第二个字节的偶数位 合并成一个字节,你这个方法就不通用了吧,要怎么做呢???
#4
可以嵌入汇编,连ZF寄存器一起Rorate……
#5
data[1]: 00010 000
data[2]: 00101 100
usigned char res = (unsigned char)(data[2]>>3)||(unsigned char)(data[1]&0xE0);
data[2]: 00101 100
usigned char res = (unsigned char)(data[2]>>3)||(unsigned char)(data[1]&0xE0);
#6
usigned char res = (unsigned char)(data[2]>>3)|(unsigned char)(data[1]&0xE0);
#7
unsigned char d= (unsigned char)(data[1]<<5)+(unsigned char)data[2]>>3;
#8
是低三位看错了
usigned char res = (unsigned char)((data[2]>>3)&0x1F)|(unsigned char)((data[1]<<5)(&0xE0);
usigned char res = (unsigned char)((data[2]>>3)&0x1F)|(unsigned char)((data[1]<<5)(&0xE0);
#9
马的 怎么这么难编辑
usigned char res = (unsigned char)((data[2]>>3)&0x1F)|(unsigned char)((data[1]<<5)&0xE0);
usigned char res = (unsigned char)((data[2]>>3)&0x1F)|(unsigned char)((data[1]<<5)&0xE0);
#10
d = (d[1]&0x0f)<<5 + (d[2]&0xf8)>>3;
#11
“移位运算符(<<和>>)”的优先级比“位与(&)”的优先级高~
#12
位操作,邏輯移位!
#13
unsigned char ch3 = (((unsigned char)ch1 << 5) | ((unsigned char)ch2 >> 3));
#14
联合体真的是用的好,高实在是高!
向CSDN论坛小助手的开发者致敬!
向CSDN论坛小助手的开发者致敬!
#15
unsigned char c = (data&0x07) + (data>>3);
#16
c = ((data&0x07)<<5) + (data>>3);
#17
ch1 = 7&data[2];
ch1 = ch1<<5;
ch2 = 248&data[3];
ch2 = ch2>>3;
ch = ch1|ch2;
ch1 = ch1<<5;
ch2 = 248&data[3];
ch2 = ch2>>3;
ch = ch1|ch2;
#18
使用c语言中的位,如下
union u
{
struct s
{
unsigned L3:3;
unsigned L5:5;
unsinged h3:3;
unsigned h5:5;
}a;
unsigned char x;
unsigned char y;
};
.....
unsigned int m;
tmp.x=16;tmp.y=44;
m=tmp.a.l3<<5+tmp.a.h5;
....
union u
{
struct s
{
unsigned L3:3;
unsigned L5:5;
unsinged h3:3;
unsigned h5:5;
}a;
unsigned char x;
unsigned char y;
};
.....
unsigned int m;
tmp.x=16;tmp.y=44;
m=tmp.a.l3<<5+tmp.a.h5;
....
#19
data = (data[1]<<5)||(data[2]>>3)
#20
奇偶重组的问题更简单:
unsigned a=16;// 取它的奇数位
unsigned b=44;// 取它的偶数位
unsigned c=0;// 存放新组的字节
c=(a&0x55)+(b&0xaa);
unsigned a=16;// 取它的奇数位
unsigned b=44;// 取它的偶数位
unsigned c=0;// 存放新组的字节
c=(a&0x55)+(b&0xaa);
#21
更正:
a,b,c都申明为char类型,
c=(a&0x55)+(b&0xaa);
其实一下没两个写法都可以,各位自己想一想是为什么
c=(a&0x55)|b;
c=a|(b&0xaa);
a,b,c都申明为char类型,
c=(a&0x55)+(b&0xaa);
其实一下没两个写法都可以,各位自己想一想是为什么
c=(a&0x55)|b;
c=a|(b&0xaa);
#22
不好意思,
c=(a&0x55)|b;
c=a|(b&0xaa);
是不行的
c=(a&0x55)|b;
c=a|(b&0xaa);
是不行的
#1
union{
struct {
unsigned char x;
unsigned char y;
}a;
unsigned short b;
}temp;
int main(){
temp.a.x=16; //00010 000
temp.a.y=44; //00101 100
temp.b >>= 3;
cout << (int)temp.a.y << endl; //输出5 //000 00101
return 0;
}
struct {
unsigned char x;
unsigned char y;
}a;
unsigned short b;
}temp;
int main(){
temp.a.x=16; //00010 000
temp.a.y=44; //00101 100
temp.b >>= 3;
cout << (int)temp.a.y << endl; //输出5 //000 00101
return 0;
}
#2
楼长快揭帖呀.求求你了.
#3
呵呵,楼上的把union玩的出神入化,小弟开眼了。
但是,如果要求将 第一个字节的奇数位 和 第二个字节的偶数位 合并成一个字节,你这个方法就不通用了吧,要怎么做呢???
但是,如果要求将 第一个字节的奇数位 和 第二个字节的偶数位 合并成一个字节,你这个方法就不通用了吧,要怎么做呢???
#4
可以嵌入汇编,连ZF寄存器一起Rorate……
#5
data[1]: 00010 000
data[2]: 00101 100
usigned char res = (unsigned char)(data[2]>>3)||(unsigned char)(data[1]&0xE0);
data[2]: 00101 100
usigned char res = (unsigned char)(data[2]>>3)||(unsigned char)(data[1]&0xE0);
#6
usigned char res = (unsigned char)(data[2]>>3)|(unsigned char)(data[1]&0xE0);
#7
unsigned char d= (unsigned char)(data[1]<<5)+(unsigned char)data[2]>>3;
#8
是低三位看错了
usigned char res = (unsigned char)((data[2]>>3)&0x1F)|(unsigned char)((data[1]<<5)(&0xE0);
usigned char res = (unsigned char)((data[2]>>3)&0x1F)|(unsigned char)((data[1]<<5)(&0xE0);
#9
马的 怎么这么难编辑
usigned char res = (unsigned char)((data[2]>>3)&0x1F)|(unsigned char)((data[1]<<5)&0xE0);
usigned char res = (unsigned char)((data[2]>>3)&0x1F)|(unsigned char)((data[1]<<5)&0xE0);
#10
d = (d[1]&0x0f)<<5 + (d[2]&0xf8)>>3;
#11
“移位运算符(<<和>>)”的优先级比“位与(&)”的优先级高~
#12
位操作,邏輯移位!
#13
unsigned char ch3 = (((unsigned char)ch1 << 5) | ((unsigned char)ch2 >> 3));
#14
联合体真的是用的好,高实在是高!
向CSDN论坛小助手的开发者致敬!
向CSDN论坛小助手的开发者致敬!
#15
unsigned char c = (data&0x07) + (data>>3);
#16
c = ((data&0x07)<<5) + (data>>3);
#17
ch1 = 7&data[2];
ch1 = ch1<<5;
ch2 = 248&data[3];
ch2 = ch2>>3;
ch = ch1|ch2;
ch1 = ch1<<5;
ch2 = 248&data[3];
ch2 = ch2>>3;
ch = ch1|ch2;
#18
使用c语言中的位,如下
union u
{
struct s
{
unsigned L3:3;
unsigned L5:5;
unsinged h3:3;
unsigned h5:5;
}a;
unsigned char x;
unsigned char y;
};
.....
unsigned int m;
tmp.x=16;tmp.y=44;
m=tmp.a.l3<<5+tmp.a.h5;
....
union u
{
struct s
{
unsigned L3:3;
unsigned L5:5;
unsinged h3:3;
unsigned h5:5;
}a;
unsigned char x;
unsigned char y;
};
.....
unsigned int m;
tmp.x=16;tmp.y=44;
m=tmp.a.l3<<5+tmp.a.h5;
....
#19
data = (data[1]<<5)||(data[2]>>3)
#20
奇偶重组的问题更简单:
unsigned a=16;// 取它的奇数位
unsigned b=44;// 取它的偶数位
unsigned c=0;// 存放新组的字节
c=(a&0x55)+(b&0xaa);
unsigned a=16;// 取它的奇数位
unsigned b=44;// 取它的偶数位
unsigned c=0;// 存放新组的字节
c=(a&0x55)+(b&0xaa);
#21
更正:
a,b,c都申明为char类型,
c=(a&0x55)+(b&0xaa);
其实一下没两个写法都可以,各位自己想一想是为什么
c=(a&0x55)|b;
c=a|(b&0xaa);
a,b,c都申明为char类型,
c=(a&0x55)+(b&0xaa);
其实一下没两个写法都可以,各位自己想一想是为什么
c=(a&0x55)|b;
c=a|(b&0xaa);
#22
不好意思,
c=(a&0x55)|b;
c=a|(b&0xaa);
是不行的
c=(a&0x55)|b;
c=a|(b&0xaa);
是不行的