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

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    5

    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
    Last edited by mrmeng; 09-18-2010 at 11:28 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    Thank you, makes sense

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    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. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    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++) ?
    Last edited by Bayint Naung; 09-19-2010 at 02:47 AM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    Quote Originally Posted by Bayint Naung View Post
    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. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New Computer = a few questions
    By Rune Hunter in forum Tech Board
    Replies: 4
    Last Post: 11-25-2004, 10:06 AM
  2. Computer will not boot.
    By RealityFusion in forum Tech Board
    Replies: 25
    Last Post: 09-10-2004, 04:05 PM
  3. Regarding Undergraduate Computer Majors
    By UnregdRegd in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-04-2003, 11:55 AM
  4. This is my last night on this computer.
    By joshdick in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 07-12-2003, 01:33 AM
  5. Which distro should I run on my old computer?
    By joshdick in forum Tech Board
    Replies: 5
    Last Post: 04-09-2003, 01:37 AM