For example:
int main(void) {
Int i = Int(3); //3-bit i
You can't create integers of size less than char
(that is, each object has a size in bytes that's a multiple of sizeof(char)
, which is 1). But that's not a problem since you can pack numbers inside a larger number.
const unsigned size_in_bits = 3;
unsigned a = 1; // 001
unsigned b = 5; // 101
unsigned packed = (b << size_in_bits*1) | (a << size_in_bits*0); // 101001
unsigned unpacked_a = (packed >> size_in_bits*0) & ((1 << size_in_bits)-1);
unsigned unpacked_b = (packed >> size_in_bits*1) & ((1 << size_in_bits)-1);
or use bitfields (the syntax is nicer, but the binary layout is implementation-defined)
struct Date
{
unsigned day : 5;
unsigned month : 4;
unsigned year : 21;
};
Date d;
d.day = 5; d.month = 11; d.year = 2014;
No, size of every primitive element (int,short,long...) depends of hardware architecture.
For bigger sizes, you should use one Big Integer library (they represent numbers with strings).
You could always try to use integer and float manipulation using arrays. If the number is too big for array initialization, you could use the malloc();
function.
Please Note: This method is not very fast, because we will be allocating memory in the heap. You will also have to write your own mathematical operation functions, because I am not quite sure on how to implement it efficiently. SEE MORE BELOW
#include <stdio.h>
#include <stdlib.h>
#define MAX_DIGIT_COUNT 1000
int main()
{
int* big_num = (int*)malloc(sizeof(int) * MAX_DIGIT_COUNT); //allocate memory
for(int x; x<MAX_DIGIT_COUNT; x++)
{
/*
*Iterating through number - iterating works, because we are basing out max number lenght
*off of the maximum digits, and therefore, we can have a maximum of 2^64 digits
*/
big_num[x] = rand()%5; //fill up memory block with psuedo-random numbers
}
/*Printing begins here...*/
for(int i; i<MAX_DIGIT_COUNT; i++)
{
int iterated;
iterated = big_num[i];
printf("%d", iterated);
}
printf("\n");
/*Printing ends here*/
return 0;
}
Please note: This is just a crude way of implementing arbitrary-sized number support in pure C.
You could try the GNU Multiple Precision Arithmetic Library library, which supports both integers, fractions, and real numbers.
You could write a wrapper class around std::bitset
or std::vector<bool>
. These are bit containers.
Your class would contain one of the containers and add functionality for conversion to and from integral numbers; as well as other arithmetic operations.
This will allow you to have unusual bit sized integers, such as 3, 5 and 13.
Most implementations will round up to the nearest multiple of 8 or the processor's word size. A container of 3 bits would use a uint8_t
with 5 unused bits, primary because its easier for the processor to manipulate. A 13-bit integer would reside in a 16-bit package.
Edit 1: Floating Point Number
Unless you conform to the standard floating point formats, you will have to write your own wrapper class. This would allow you to have 3 bits of mantissa, 5 bits of exponent and one bit for sign -- 9bits. Again, think about all the methods you would need to write. Most applications will use either double
or float
because there is no need to write separate wrappers which takes coding time and testing time.