Method 1:

unsigned long l_val;
unsigned short s_low, s_high;
s_low = (unsigned short )l_val;
s_high = (unsigned short )(l_val >> 16);
Method 2:
struct two_short
{
  unsigned short low;
  unsigned short high;
};
unsigned long l_val;
unsigned short s_low, s_high;
struct two_short * p_long;
p_long = (Struct two_short *)&l_val;
s_high = p_long->high;
s_low = p_long->low;
Method 3:
unsigned long l_val;
unsigned short s_low, s_high;
unsigned short * p_short;
p_long = (unsigned short *)&l_val;
s_low = *p_short;
p_short++;
s_high = *p_short;
Please note method 3 only works for only little endian processors.

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

int to string, reverse decimal digits, signed and unsigned compare, palindrome digits, upper and lower 16bit of 32bit unsigned, Exchange variables, prime number, HCF,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan