# Thread: Studying computer architecture without knowledge of C-programming, got some questions

1. ## Studying computer architecture without knowledge of C-programming, got some questions

I'm on a foreign exchange program and studying computer architecture at university. The problem is this university it assumes the students have studied C-programming as their course in basic programming, whereas at my regular university the course in basic programming is ADA.

I am trying to catch up but need some help since I don't really know how C-language is structured.

My current problem:

Shift operations, 32-bit.
Got two functions

int func1 (unsigned word)
{
return (int) ((word<<24) >> 24);
}
int func2 (unsigned word)
{
return ((int) word <<24) >> 24;
}

How does these functions work? I understand the logical shift but not the functions.

My guess is func1 first shift all bits 24 bits to left, and then 24 bits to right, resulting in the 8 LSB will be the same but the 24 MSB will turn into 0's.

Even if my guess is correct I can't see how func2 would work differently. I would greatly appreciate if someone could ellaborate for me.

Examples with 127, 128, 255, 256 as in values?

EDIT: Represented by two's complement

2. > return ((int) word <<24) >> 24;
If the LSByte of word is 0x80 to 0xFF, then the MSBit will end up as the sign bit.

Right-shifting signed numbers is implementation-defined in C, meaning you either get zeros or the sign bit.

The result would be say
0x00000080
or
0xFFFFFF80

func1 as you said will always fill with zeros.

3. Thank you, makes sense

4. Stumbled upon another problem.

This code is supposed to return 0 when "len" = 0, but it returns memory access exception. Why does this occur and how do I fix it?

float sum_elements(float a[], unsigned len)
{
int i;
float result=0;

for (i=0; i <=len-1; i++)
result += a[i];

return result;
}

5. Because len is declared as unsigned.

Code:
`for (i=0; i <=len-1; i++)      // signed and unsigned comparison`
I think you should go and read some books on C...
Btw, valid indices are 0 to array_elements-1 inclusive. So shouldn't you doing for(i = 0; i < len; i++) ?

6. First, you should read: << !! Posting Code? Read this First !! >>
Second: Unsigned integers can only hold >= 0. Therefore, if len is 0 and you subtract 1, you will get 0xFFFFFFFF, which is -1 in two complement. But since it isn't unsigned, you get the unsigned representation.
Third: <len and <=len - 1 are the same thing. It's a matter of style.

7. Originally Posted by Bayint Naung
Because len is declared as unsigned.

Code:
`for (i=0; i <=len-1; i++)      // signed and unsigned comparison`
I think you should go and read some books on C...
Thank you.

I am studying computer architecture with the 2nd year students and will enroll the course in C programming with the freshmen when they start. I am trying to learn as much as I can on my own but at the moment I need to focus on my other courses, so I greatly appreciate the help since I have very limited knowledge on C.

I'm pretty much studying 24/7, I'm in China so all courses are taught in Chinese. Needless to say it's quite difficult and time consuming, it will be easier when my course in C programming begins but as for now I appreciate all help I can get from this forum, just to keep my head above the water.

I know I am asking newbie questions, please bare with me. My course in programming starts in two weeks

8. I got this function, could someone explain to me what it does?

Code:
```int sum_array ( int a [N] [N] [N] )
{
int i, j, k, sum = 0;
for (i=0; i < N; i++)
for (j=0; j < N; j++)
for (k=0; k < N; k++) sum+=a[k] [i] [j]
return sum;
}```
I'm supposed to change the cycle order to make the array elements consistent with the order of access in main memory (current order is from left to right), how do I do this and why?