C array variables with macros

Is it possible to create a specific permutation of an array with a macro in C?

i.e. If I have an X array with elements:

0 1 2 3 4 5 x = ["0","1","1","0","1","0"] 

I thought there might be a foo macro for something like this:

 #define S_2Permute(x) = [x[5], x[3], x[4], x[2], x[1]] 

where I redefine the order of the array, so the element in starting position 5 is now at position 0.

Any ideas?

EXAMPLE OF USE

I am starting to create an implementation of the DES encryption algorithm. DES requires several permutations / extensions, in which I will have to reorder all elements of the array, sometimes shrinking the array and sometimes expanding it. I was hoping to just define a macro to move arrays for me.

EDIT2

Well, in DES, the first step is something called an initial permutation. Therefore, initially I have a 64-bit key, which for this example can be 0-15 hex:

 0123456789ABCDEF 

which expands to:

 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 

IP (initial permutation) will rearrange this line so that each element of the array is in a new position:

 IP = 58 50 42 34 26 18 10 2 60 52 44 36 28 20 12 4 62 54 46 38 30 22 14 6 64 56 48 40 32 24 16 8 57 49 41 33 25 17 9 1 59 51 43 35 27 19 11 3 61 53 45 37 29 21 13 5 63 55 47 39 31 23 15 7 

Thus, the new 1st element in the bit string will be the 58th element (bit) from the original bit string.

So, I would have all these bits stored in an array of characters:

 x = [0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,0,1,0,1,0,1,1,0,0, 1,1,1,1,0,0,0,1,0,0,1,1,0,1,0,1,0,1,1,1,1,0,0,1,1,0,1,1,1,1,0,1,1,1,1] 

and then just call

 IP_PERMUTE(x); 

And macro magic will move all the bits to the new right positions.

+6
source share
3 answers

Absolutely - you are almost on your own example. Try the following:

  #define S_2Permute(x) {x[5], x[3], x[4], x[2], x[1]} 

Then later:

 int x[] = {1,2,3,4,5,6}; int y[] = S_2Permute(x); // y is now {6,4,5,3,2} 

Two things to remember:

1) in C, arrays are numbered from 0, so you could keep in mind:

 #define S_2Permute(x) {x[4], x[2], x[3], x[1], x[0]} 

2) If you use gcc, you can compile with -E to see the output from the preprocessor (very good for debugging macros).


However, I don’t think I actually do it this way: I would say that the code will be easier to read (and perhaps less error prone) if you programmatically create permutations - and I doubt it will be a great success.


Since you are saying that you are having trouble compiling this, here is a test program that works for me in gcc 4.6.1:

 #include <stdio.h> #define S_2Permute(x) {x[5], x[3], x[4], x[2], x[1]} int main(void) { int x[] = {1,2,3,4,5,6}; int y[] = S_2Permute(x); for(int i = 0; i < 5; i++) { printf("%d,",y[i]); } printf("\n"); } 

I compiled with gcc test.c -std=c99 -Wall

+4
source

I am new, so I apologize if it’s not good to offer different solutions, but do you consider using a built-in function instead of a macro?

I like single lines of code that do a lot more than the next guy, but it makes sense to me to do it like this:

 //I would have an array that defined how I wanted to swap the positions, I'll assume 5 elements short reordering[5] = {4,1,3,2,0}; inline void permuteArray(char array[]) { char swap = array[reordering[0]]; array[reordering[0]] = array[reordinger[1]]; array[reordering[1]] = array[reordinger[2]]; array[reordering[2]] = array[reordinger[3]]; array[reordering[3]] = array[reordinger[4]]; array[reordering[4]] = swap; } 

It may not be as beautiful or effective as a macro, but it can save you some headaches that control and support your code (and you can always swap them for the version of the macro Timothy offers.

+2
source

I am doing something similar. This is my code. The variable included in it is ulung, so I convert it to a bit array, and then rearrange all the bits, and then return it back to ulung.

 public override ulong Permutation(ulong input, int[] permuation) { byte[] test = BitConverter.GetBytes(input); BitArray test2 = new BitArray(test); BitArray final = new BitArray(test); ulong x = 0; ulong y = 1; for (int i = 0; i < permuation.Length; i++) { final[i] = test2[(permuation[i]-1)]; } for (int i = 0; i < final.Length; i++) { if (final[i] == true) { x += (1 * y); } else { x += (0 * y); } y = y * 2; } return x; } 
0
source

Source: https://habr.com/ru/post/908002/


All Articles