[英]using pointers to select numbers within a long unsigned variable
I am required to add two select only two numbers from a Variable ie 0X11223344 and I want my pointers to pick 22 in the middle of the array.我需要从变量中添加两个 select 仅两个数字,即 0X11223344,我希望我的指针在数组中间选择 22。 how do I go about it我该怎么做 go 关于它
You can use shift and modulo operations to get the value您可以使用移位和模运算来获取值
int main(){
return (0X11223344 >> 16) % 256;
}
The program returns 34 == 0x22程序返回 34 == 0x22
A right shift of 4 removes 1 digit.右移 4 会删除 1 个数字。 A right shift of 16 removes 4 digits.右移 16 位会删除 4 位数字。 A modulo of 16 removes all but one digits.模 16 会删除除一位之外的所有数字。 A modulo of 16*16= 256 removes all but 2 digits. 16*16= 256 的模数会删除除 2 位之外的所有数字。
You can also get the value with pointer operations:您还可以通过指针操作获取值:
int main() {
int endianness = 2;
int a = 0x11223344;
char *b = ((char *) &a) + endianness;
return *b;
}
The value of endianess
is implementation defined. endianess
的值是实现定义的。 On a system with little endiannes it's 2在带有小端序的系统上,它是 2
|01 02 03 04| memory address
-------------
|44 33 22 11| 4 byte int with address 01 and value 0x11223344
| | |22| | 1 byte char with address 03 and value 0x22
and on a system with big endianness it's 1在具有大字节序的系统上,它是 1
|01 02 03 04| memory address
-------------
|11 22 33 44| 4 byte int with address 01 and value 0x11223344
| |22| | | 1 byte char with address 02 and value 0x22
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.