Saturday, June 29, 2013

To Write and Read the 32 bit Variable using 4 Byte Array

/*
 * Read_Array.c
 *
 *  Created on: Jun 29, 2013
 *      Author: Karthik
 */


/* To Write the 32 bit variable using 4 Byte Array*/

#include "stdio.h"
#include "stdlib.h"

int main()
{
    /* variable Initialization */

    int nArray[4] = {1,0,0,0};
    //int nOutVar = 0;
    unsigned int nlOutVar=0;

    printf("%d\n",nArray[0]);
    printf("%d\n",nArray[1]);
    printf("%d\n",nArray[2]);
    printf("%d\n",nArray[3]);
    nlOutVar = nArray[0];
    nlOutVar = nlOutVar << 8;
    printf("2byte usage%u\n",nlOutVar);
    nlOutVar = nlOutVar + nArray[1];
    nlOutVar = nlOutVar << 8;
    printf("3byte usage%u\n",nlOutVar);
    nlOutVar = nlOutVar + nArray[2];
    nlOutVar = nlOutVar << 8;
    printf("4byte usage%u\n",nlOutVar);
    nlOutVar = nlOutVar + nArray[3];

    printf("4byte content looks like %u\n",nlOutVar);

    nlOutVar = nlOutVar >> 16;
    printf("MSB first 2byte content reading %u\n",nlOutVar);

    nlOutVar = 4294967295;
    printf("maximum value %u\n",nlOutVar);

    return 0;

}

Output:

1
0
0
0
2byte usage256
3byte usage65536
4byte usage16777216
4byte content looks like 16777216
MSB first 2byte content reading 256
maximum value 4294967295

No comments:

Post a Comment