简体   繁体   中英

How to use 64-bit integers in 32-bit compiler?

I am currently writing a program to run on a Microchip PIC18 MCU. I use xc8 (v1.45 -I have to use this version-) compiler and working in MPLAB IDE. In this version of the compiler, there is no 64-bit integer support. I need to use 64-bit integers for some calculations. Please see my method below to create 64-bit integer variable type. But whenever I try to convert any other variable type to this newly created typte I got this error:

error: cast to union type from type not present in union

Could you please help me to fix this problem?

I have tried to use "long long int" variable but, this version of xc8 doesn't support it.

#include <stdio.h>
#include <stdint.h>

typedef union
{
    int32_t bigInteger[2];
}myInt64;


int main(void) {

    myInt64 *myVariable;

    myInt64 *aaa;

    long abc = 0xFAC0ED12;

    aaa = (myInt64)abc;

    myVariable = 0xF0000000000000000F;
    printf("%jx", aaa);


    return EXIT_SUCCESS;
}

You cannot make a 64-bit integer type simply by making a structure or a union that contains 64 bits, regardless of what the internal members of that structure or union are. C has no provisions for converting integers to such a type or for doing any arithmetic on them.

To implement 64-bit integers without direct support from the compiler, you must write your own routines to do whichever operations you need, potentially including:

  • Given two 32-bit integers that represent a 64-bit number in “base 2 32 ”, convert them to your 64-bit type.
  • Convert from your 64-bit type to two 32-bit integers.
  • Convert from a numeral in a string to your 64-bit type (as scanf does for built-in types).
  • Convert from your 64-bit type to a numeral (as printf does).
  • Add, subtract, multiply, and divide two operands of your 64-bit type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM