0

I want to initialize an int array with constant values, with all the 2^n values from 2^0 to 2^31 and I want to know which method below is right and if it gives the result I want and if there are easier or shorter methods to initialize it.

static const char     two_n[32];

two_n[32] = {1, 2 ,4 ,8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096,
               8192, 16384, 32768, 65535, 131070, 262140, 524280, 1048560,
               2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728,
               268435456, 536870912, 1073741824, 2147483648};

or

static const char     *two_n[32];

two_n[32] = {1, 2 ,4 ,8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096,
               8192, 16384, 32768, 65535, 131070, 262140, 524280, 1048560,
               2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728,
               268435456, 536870912, 1073741824, 2147483648};

or

static const int     two_n[32];

two_n[32] = {1, 2 ,4 ,8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096,
               8192, 16384, 32768, 65535, 131070, 262140, 524280, 1048560,
               2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728,
               268435456, 536870912, 1073741824, 2147483648};
5
  • 3
    None of these will work. Did you just try the code and see the results? You need to review your understanding of arrays. Try something simpler. Commented Nov 27, 2016 at 21:49
  • 65535 is wrong along with several more after that. You'd be better off filling the array with a simple for loop when the program starts. Or you could use hexadecimal notation for the constants, since the hexadecimal values follow a nice obvious pattern. Commented Nov 27, 2016 at 21:52
  • Thanks for the comments, I'm in an it school where using the for loops is forbidden in first year, and I can't use hexadecimal because I need to do a program that sorts numbers using Radix, and I am converting numbers to binary base then sorting using ascending binary order. Commented Nov 27, 2016 at 22:26
  • the values for the powers of 2 are given by: static const int two_n[32] = { 1<<0, 1<<2, 1<<3, 1<<4,..... 1<<31 };` Commented Nov 29, 2016 at 12:07
  • Note: after a const array is declared, it cannot be assigned to. (otherwise it would not be const.) Commented Nov 29, 2016 at 12:09

1 Answer 1

3

Neither of the above because both the types of the arrays and the initializers are incorrect. Use uint32_t or unsigned long as these are guaranteed to be at least 32-bit wide and able to hold the value of 231, which might be beyond the range of int:

static const unsigned long two_n[32] = {
    1UL <<  0, 1UL <<  1, 1UL <<  2, 1UL <<  3,
    1UL <<  4, 1UL <<  5, 1UL <<  6, 1UL <<  7,
    1UL <<  8, 1UL <<  9, 1UL << 10, 1UL << 11,
    1UL << 12, 1UL << 13, 1UL << 14, 1UL << 15,
    1UL << 16, 1UL << 17, 1UL << 18, 1UL << 19,
    1UL << 20, 1UL << 21, 1UL << 22, 1UL << 23,
    1UL << 24, 1UL << 25, 1UL << 26, 1UL << 27,
    1UL << 28, 1UL << 29, 1UL << 30, 1UL << 31
};
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the comment, I'll use long type and not unsigned because I'll also need negative values.
@MohammedBRAHMI: If long type is 32-bit wide, it cannot hold 2^31. You would need long long which is guaranteed to hold at least up to 2^63-1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.