Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struct initialisation with large integer

Tags:

c

So I have the following struct:

typedef struct int64 {
    unsigned char value[8];
} int64;

Which I an using to represent a 64 bit integer (I know this exists in stdint.h but I thought it could be a good exercise to try and write it myself, and I plan to use this format for much larger integers). My question is there any way that I could initialize this struct with a binary string or an overlarge integer, something like:

int64 number = 0b1000000000000000000000000000000000000000000000000000000000011001

// Or

int64 number = 1231823812738123878;  // Or something larger than 2^32

Thanks for any help you can give me :)

like image 946
Nathcat Avatar asked Dec 11 '25 04:12

Nathcat


1 Answers

You'd have to break it up byte by byte, since the struct contains an array of bytes:

int64 number = { { 0b10000000, 0b00000000, 0b00000000, 0b00000000, 
                   0b00000000, 0b00000000, 0b00000000, 0b00011001 } };

You can compress this using hex constants instead of binary constants:

int64 number = { { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19 } };
like image 181
dbush Avatar answered Dec 13 '25 20:12

dbush



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!