0

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;
}
phuclv
  • 37,963
  • 15
  • 156
  • 475
  • 3
    Why do you have a union with just one member? – Barmar Oct 03 '19 at 18:42
  • You should be getting warnings from this code. `myVariable` and `aaa` are pointers, but you're assigning integers to them. – Barmar Oct 03 '19 at 18:44
  • I try to allocate 64-bit memory space for this union. Because -as I know- unions take place in memory as their biggest sized members. – İ. Alkuş Oct 03 '19 at 18:45
  • 1
    Why not use a `struct` instead? – Barmar Oct 03 '19 at 18:45
  • 3
    If the compiler has no 64-bit int type, it probably doesn't support 64-bit int literals either. – 500 - Internal Server Error Oct 03 '19 at 18:46
  • @Barmar I don't know how to use struct here :/ – İ. Alkuş Oct 03 '19 at 18:50
  • `typedef struct { int32_t bigInteger[2]; } myInt64;` – Barmar Oct 03 '19 at 18:50
  • 1
    If your compiler is C99 compliant, it will provide `int_least64_t`. – Pierce Griffiths Oct 03 '19 at 18:51
  • 2
    @PierceGriffiths: And `long long` would work just as well. – R.. GitHub STOP HELPING ICE Oct 03 '19 at 18:57
  • See the discussion in https://stackoverflow.com/questions/269268/how-to-implement-big-int-in-c as well as https://stackoverflow.com/questions/23038451/how-does-a-32-bit-processor-support-64-bit-integers as well as https://stackoverflow.com/questions/2810280/how-to-store-a-64-bit-integer-in-two-32-bit-integers-and-convert-back-again as well as https://stackoverflow.com/questions/3190143/how-is-64-bit-math-accomplished-on-a-32-bit-machine – Richard Chambers Oct 03 '19 at 19:10
  • See also [UINT64 on XC8](https://www.microchip.com/forums/m837247.aspx) – chux - Reinstate Monica Oct 03 '19 at 23:16
  • @500-InternalServerError: A requirement which, because it's mandatory rather than a strongly recommended feature, simply makes C99 impractical to support on some platforms (including all non-two's-complement platforms, btw). There's no reason C shouldn't be usable to write programs targeting a tiny microcontroller to do things that are possible on that controller, even if many other common things would be unsupportable on such an implementation. – supercat Dec 15 '19 at 21:48

1 Answers1

1

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 232”, 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.
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312