From: suresh babu [mailto:[EMAIL PROTECTED]
>hi, i am suresh. in an interview i was asked to reverse two
>nibbles in a byte of data.
>
>the question is as follows. suppose a number is assigned to a
>character like
>
>char c=0x82 then, a function should be described such that it
>will return number as 0x28
>
>my way of approach is as follows
[snip]
>but in the above program, temp1 is taking one byte of memory n
>value stored in it is 02, but temp2 is taking 2 bytes of
>memory and storing value as FFF8 before shifting.
>
>i had tried this program using Turboc3 compiler
>
>pls suggest modification in my program r any other approach to
>solve this problem
Your compiler is using 'signed char' - you either need to find an option
to make it use 'unsigned char' or explicitly define it:
$ type c.c
#include <stdio.h>
unsigned char exch_nibbles(unsigned char c){
unsigned char temp1, temp2;
temp1=c & 0x0F; //to obtain last four bits
temp2=c & 0xF0; //to obtain first four bits
temp1=temp1 << 4; //shifting the last four bits to left
temp2=temp2 >> 4; //shifting the first four bits to right
printf("\nThe temp1 value after shifting is %x",temp1);
printf("\nThe temp2 value after shifting is %x",temp2);
return(temp2|temp1); //adding the bits
}
int main(void){
char c=0x82;
printf("\nThe exchanged value is %x",exch_nibbles(c));
return 0;
}
$ c
The temp1 value after shifting is 20
The temp2 value after shifting is 8
The exchanged value is 28
$
--
PJH
If Windows is the solution, can we please have the problem back?
unknown
Alderley plc, Arnolds Field Estate, The Downs, Wickwar, Gloucestershire, GL12
8JD, UK
Tel: +44(0)1454 294556 Fax: +44 (0)1454 299272
Website : www.alderley.com Sales : [EMAIL PROTECTED] Service : [EMAIL
PROTECTED]
This email and its contents are confidential and are solely for the use of the
intended recipient. If you are not the original recipient you have received it
in error and any use, dissemination, forwarding, printing or copying of this
email is strictly prohibited. Should you receive this email in error please
immediately notify [EMAIL PROTECTED]
This email has been scanned for viruses, however you should always scan emails
with your own systems prior to opening.
------------------------ Yahoo! Groups Sponsor --------------------~-->
$4.98 domain names from Yahoo!. Register anything.
http://us.click.yahoo.com/Q7_YsB/neXJAA/yQLSAA/EbFolB/TM
--------------------------------------------------------------------~->
To unsubscribe : [EMAIL PROTECTED]
Yahoo! Groups Links
<*> To reply to this message, go to:
http://groups.yahoo.com/group/Programmers-Town/post?act=reply&messageNum=4971
Please do not reply to this message via email. More information here:
http://help.yahoo.com/help/us/groups/messages/messages-23.html
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/Programmers-Town/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/