integerbitwise-operatorscodesys

Combining four 16 bit UINT to one 64 bit UINT in Codesys


I'm trying to combine four 16 bit UINT to a single 64 bit UINT in codesys. I am able to do this in C but I haven't been able to figure out the syntax in Codesys. I have four elements of an array that are 16 bit UINT values.

If anyone knows a way to combine these please let me know. Thanks a lot.

VAR
     // Array
     UTCdataArray : ARRAY [0..3] OF UINT;     

     //16 bit UINT elements
     UTCval0: UINT := 0;
     UTCval1: UINT := 0;
     UTCval2: UINT := 0;
     UTCval3: UINT := 0;

     // 64 bit ULINT
     UTCval: ULINT := 0;
END_VAR


// 16 bit UINT array
UTCval0 := UTCdataArray[0];
UTCval1 := UTCdataArray[1];
UTCval2 := UTCdataArray[2];
UTCval3 := UTCdataArray[3];

// Combine 16 bit UINT array elements into 64 bit ULINT

//This is something I would do in C
UTCval := UTCval0 << 0;
UTCval := UTCval1  << 16;
UTCval := UTCval2  << 32;
UTCval := UTCval3  << 48;

Solution

  • I think bit shift left (SHL) is what you're looking for.

    UTCval := UINT_TO_ULINT(UTCval0) + SHL(UINT_TO_ULINT(UTCval1),16) + SHL(UINT_TO_ULINT(UTCval2),32) + SHL(UINT_TO_ULINT(UTCval3),48);