im trying to implement bit shifting to bigints. The BigInt is represented by an array of bytes that should be interpreted as a single integer N bits in two’s complement. So i want to make something like:
Example array bigint:
{0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} would represent the integer 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE (-2) N = 128 bits.
typedef unsigned char *BigInt;
when i try to create my bigint im getting errors
#include <stdlib.h>
void bi_init (int nbits)
{
nbytes = (nbits/8);
}
BigInt bi_new (int val)
{
BigInt new = (BigInt)malloc(nbytes*sizeof(unsigned char));
new=val --> problem, can someone give me a hint on how can i implement this array?
return novo;
}
You declare
newas anunsigned char**, that’s one level of pointers too many (and you shouldn’t cast the result ofmallocin C).Assuming that
novoandneware the same variable and only one occurrence has been translated for the post,novo=val
overwrites the just allocated address with the passed-in
val.To fill the allocated buffer with the bytes of
val,Then there remains the problem
that you’re allocating too little memory if
nbitsis not a multiple of 8, make that