# [SOLVED]help in arrays in mikroc

Status
Not open for further replies.

#### Strike_UN

##### Junior Member level 1
hi every 1
iam beginner in mikroc so iam having problems using arrays in mikroc and now iam stuck in 1 of it
my problem now is i want to put result of 32-bit of hexadecimal form to array of 4 element each element must be 8bit only
to do xor later with another array of same length
for example i got result after some mathematics operation x=0x3f434f4e and i want to put it in array to look like that y=[3f,43,4f,4e]
any help will be appreciated and thx in advance

Strike_UN

### Strike_UN

Points: 2

#### Strike_UN

##### Junior Member level 1
ok i readed ur other posts and i got it and thats my test program

Code C - [expand]1
2
3
4
5
6
7
8
9
10
11
12
long x=0x3fa2345e;
long tmp;
void main() {
while(1){
union{
long y2;
short int y[4];
}mu, *pm;
mu.y2=x;
tmp=mu.y2;
}
}

the only problem left for me is instead of y=[3f,A2,34,5E] it write it opposite y=[5E,34,A2,3F] how i solve that and thx again

Last edited by a moderator:

#### alexan_e

Would it be a problem to read the array in the opposite direction when you want to use it?

For example

Code C - [expand]1
2
3
4
for(i = 0; i < 4; i++)
{
array[3 - i]....
}

Alex

Strike_UN

### Strike_UN

Points: 2

#### Strike_UN

##### Junior Member level 1
well its just that the array will be part of big calculations and i didnot want to make it more complex with pointers just to re-organize it but since there isnot much option left i will just do it as u said
and thanks alot for the help

#### alexan_e

The array can be inverted if you assign it using shift operation instead of union

Code C - [expand]1
2
3
4
5
6
7
8
9
10
11
12
long x=0x3fa2345e;
short int y[4];

void main() {

y[0]= (x & 0xff);
y[1]= ((x>>8) & 0xff);
y[2]= ((x>>16) & 0xff);
y[3]= ((x>>24) & 0xff);

.....
}

Strike_UN

### Strike_UN

Points: 2

#### Strike_UN

##### Junior Member level 1
i didnot know such operation existed
more simple this way thanks alot

#### alexan_e

have you used shift operators?

suppose that you have a value of 0b11110000,
Code:
(0b11110000>>1) results in 0b[COLOR="#FF0000"]0[/COLOR]1111000
(0b11110000>>4) results in 0b[COLOR="#FF0000"]0000[/COLOR]1111

the bits are shifted to the right and zeros fill the left side bits that are left empty.
Note that when you apply shift operation in signed numbers you may get in trouble becasue of the sign, some compilers will preserve it and some will not.

Code:
long x=0x3fa2345e;
(x>>8) results in 0x[COLOR="#FF0000"]00[/COLOR]3fa234;
(x>>16) results in 0x[COLOR="#FF0000"]0000[/COLOR]3fa2;
(x>>24) results in 0x[COLOR="#FF0000"]000000[/COLOR]3f;

then you AND the values with 0xff to mask all bits except from the last 8,this is not required since the result will be assigned to a byte variable but it is a good practice anyway.

---------- Post added at 00:38 ---------- Previous post was at 00:36 ----------

The same applies in the shift left operator << but this time zeros are shifted in from the left.
The shift operation can also be used for division and multiplication, each shift right is a division by 2 and each shift left is a multiplication by 2.

Strike_UN

### Strike_UN

Points: 2

#### Strike_UN

##### Junior Member level 1
ye i used it it solved my problem easily
now i understand wht it exactly do that sure will come in handy alot
thx alot for the info :smile:

Last edited:

Status
Not open for further replies.