Thread: Set of questions.

  1. #16
    Registered User
    Join Date
    May 2006
    Posts
    151
    Ok Heres another question:
    "Write a function, unsigned char exchange(unsigned char in),
    which exchanges the right 4 bits of an unsigned char with its left 4 bits (e.g. exchange(0x54) will return 0x45.)"

    What would be the logic of the question. I mean for this I want to know what is unsigned char. What would be the value of unsigned char. If that is know I know how to use bit operations.

  2. #17
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    Quote Originally Posted by Ron View Post
    Ok Heres another question:
    "Write a function, unsigned char exchange(unsigned char in),
    which exchanges the right 4 bits of an unsigned char with its left 4 bits (e.g. exchange(0x54) will return 0x45.)"

    What would be the logic of the question. I mean for this I want to know what is unsigned char. What would be the value of unsigned char. If that is know I know how to use bit operations.
    unsigned char should be 8 bits

  3. #18
    Registered User
    Join Date
    May 2006
    Posts
    151
    So u mean to say if unsigned char value is 00000045, it should be swapped as 45000000. Is this what the question is asking?

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Ron View Post
    So u mean to say if unsigned char value is 00000045, it should be swapped as 45000000. Is this what the question is asking?
    No, the question is saying that if the unsigned char is 01000101, it should become 01010100. Eight bits, which you are swapping four-for-four (not reversing two-at-a-time, as you seem to have done).

  5. #20
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    It may be worth noting that 0x00 indicates that the number is in hexadecimal format.

    0x45 is 1 byte or 8 bits. The 4 is 4 bits and the 5 is 4 bits.

    0x4 = 0100
    0x5 = 0101

    0x45 = 01000101

  6. #21
    Registered User
    Join Date
    May 2006
    Posts
    151
    so by just doing x=y<<2^2, swap or would it just shift the last 4 digits leaving the rest as 0?
    eg:01000101 swap as in 01010100
    or
    01000101 shift as in 01010000


    Also, how do I arrange words in ascending order in an array without having to compare each character?
    Last edited by Ron; 08-08-2008 at 03:22 PM.

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Ron View Post
    so by just doing x=y<<2^2
    Do you mean y << 4 here? (Since y << 2^2 is something rather different.) Yes, that would only shift, but you need to swap.

  8. #23
    Registered User
    Join Date
    May 2006
    Posts
    151
    Hmmmm
    so can I do this.... x=y<<4
    z=y>>4;
    y=x+z;
    its very amateurish.....

    Also, how do I arrange words in ascending order in an array without having to compare each character?

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Ron View Post
    Hmmmm
    so can I do this.... x=y<<4
    z=y>>4;
    y=x+z;
    its very amateurish.....
    That's the easy way to do it.
    Quote Originally Posted by Ron View Post
    Also, how do I arrange words in ascending order in an array without having to compare each character?
    Stop comparing when you find that the two words are different.

  10. #25
    Registered User
    Join Date
    May 2006
    Posts
    151
    Did not understand, I am comparing a list of different words and arranging them in ascending order. How can I do this without comparing each character?

  11. #26
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    Quote Originally Posted by Ron View Post
    Did not understand, I am comparing a list of different words and arranging them in ascending order. How can I do this without comparing each character?
    You don't need to compare every character:

    adam == eve ?
    a == e ? no! done!

  12. #27
    Registered User
    Join Date
    May 2006
    Posts
    151
    How will that put the words in ascending order? Im sure strcmp does not have > or < .

  13. #28
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    Quote Originally Posted by Ron View Post
    How will that put the words in ascending order? Im sure strcmp does not have > or < .
    Maybe you should look at what strcmp does then.

  14. #29
    Registered User
    Join Date
    May 2006
    Posts
    151
    Ooops

  15. #30
    Registered User
    Join Date
    May 2006
    Posts
    151
    How can I create a circular array , push the new strings from rear and pop the first strings?

    eg: char *s[10]; Like a queue, but with no nodes?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 10:05 AM
  2. C help for network animator
    By fastshadow in forum Tech Board
    Replies: 7
    Last Post: 03-17-2006, 03:44 AM
  3. Find all possible subset of a given set
    By Downnin in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 02:03 PM
  4. Program help
    By Trebor in forum C++ Programming
    Replies: 4
    Last Post: 06-04-2002, 03:19 PM