L
lovecreatesbeauty
/*
It seems that when an int with width of four bytes is assigned to a one
byte width char, the first three bytes from left to right are discarded
and the rightest byte is assigned to that char.
So, I can just assign an int to a char to retrieve the rightest byte of
that int, and I also use this method plus right-shift operation to get
the leftest byte of an int.
I should be wrong on it, please correct me with your expertise.
Thank you
lovecreatesbeauty
*/
#include <stdio.h>
int main(void)
{
unsigned int data = 'chum';
unsigned char left = '\0';
unsigned char right = '\0';
printf("data: %#X\n", data);
/* get the leftest byte of an int */
left = data >> (sizeof(int) - 1) * 8;
/* get the rightest byte of an int */
right = data;
printf("left: %#X, %c\n", left, left);
printf("right: %#X, %c\n", right, right);
return 0;
}
/* result: (Win2k, mingw32)
data: 0X6368756D
left: 0X63, c
right: 0X6D, m
*/
It seems that when an int with width of four bytes is assigned to a one
byte width char, the first three bytes from left to right are discarded
and the rightest byte is assigned to that char.
So, I can just assign an int to a char to retrieve the rightest byte of
that int, and I also use this method plus right-shift operation to get
the leftest byte of an int.
I should be wrong on it, please correct me with your expertise.
Thank you
lovecreatesbeauty
*/
#include <stdio.h>
int main(void)
{
unsigned int data = 'chum';
unsigned char left = '\0';
unsigned char right = '\0';
printf("data: %#X\n", data);
/* get the leftest byte of an int */
left = data >> (sizeof(int) - 1) * 8;
/* get the rightest byte of an int */
right = data;
printf("left: %#X, %c\n", left, left);
printf("right: %#X, %c\n", right, right);
return 0;
}
/* result: (Win2k, mingw32)
data: 0X6368756D
left: 0X63, c
right: 0X6D, m
*/