|
-
April 10th, 2004, 04:30 PM
#1
Data Type Problem unsigned short
I am using data type unsigned short on two functions. An input function and an output function.
for example if users type like
Code:
unsigned short x =100;
unsigned short y;
y = input(x);
y = output(x);
cout<<y;
//the output should be 100
the problem is, the function can only take 2^(15) -1 or 32767. I can't figure out the problem. For instance if your input is over 32767, it gives wrong result.
Here is the code for both function
Code:
unsigned short Input(unsigned short usData)
{
unsigned short usOutputBit = 0;
static int nRegisterBitValue = 0;
unsigned short usResult = 0;
unsigned short usResult2 = 0;
unsigned short usInputBit;
unsigned short usBit;
int i;
usInputBit = usData;
for (i = 0; i < 16; i++)
{
usBit = (usInputBit & 0x8000) ? 1 : 0;
usOutputBit = usBit ^ nRegisterBitValue;
nRegisterBitValue = usOutputBit;
usInputBit = usInputBit << 1;
usResult = (usOutputBit | 0x8000) | usResult2;
usResult = usResult << 1;
usResult2 = usResult;
if(usInputBit == 0)
{
usResult = usResult >> 1;
}
}
return(usResult);
}
unsigned short Output(unsigned short usData)
{
unsigned short usOutputBit = 0;
unsigned short usResult2 = 0;
static int nRegisterValue = 0;
unsigned short usResult = 0;
unsigned short usInputBit;
unsigned short usBit;
int i;
usInputBit = usData;
for (i = 0; i < 16; i++)
{
usBit = (usInputBit & 0x8000) ? 1 : 0;
usOutputBit = usBit ^ nRegisterValue;
nRegisterValue = usBit;
usInputBit = usInputBit << 1;
usResult = (usOutputBit | 0x8000) | usResult2;
usResult = usResult << 1;
usResult2 = usResult;
if(usInputBit == 0)
{
usResult = usResult >> 1;
}
}
return(usResult);
}
I think the problem may be in this line of code
usResult = (usOutputBit | 0x8000) | usResult2;
which I use to pack the bit together
-
April 11th, 2004, 08:30 AM
#2
try to declare:
usInputBit, usOutputBit, usResult and usResult2
as "unsigned int" instead of "unsigned short".
-
April 11th, 2004, 08:31 AM
#3
What are the functions supposed to do? From the code, it's impossible to tell, as it does not make any sense to me.
E.g., in
usResult = (usOutputBit | 0x8000) | usResult2;
usResult = usResult << 1;
you set the high order bit (0x8000) but in the next line, you shift the bit you just set again out of usResult.
So I think, you should first tell us what input() and output() should do, before someone can really help!
BTW: The code does not compile!?
The Saviour of the World is a Penguin and Linus Torvalds is his Prophet.
-
April 11th, 2004, 09:49 AM
#4
The way to look at the function. I suppose to rename them as encoder and decoder
for instance the first function encode a number, while the second function decode the number like
Encode(13) you get 9
now if you decode 9 you get 13 back.
for instance if x = 13
and you type y=input(x)
and next y=output(y)
you should get 13 back, so this is what the functions suppose to do. However the problem I have, the highest value it can take is 32767
now I think the value if unsigned int is 32 bit, but I don't want 32 bit I only want 16 bit. So how can I fix that?
-
April 11th, 2004, 06:57 PM
#5
Code:
usInputBit = usInputBit << 1;
assume that usInputBit > 32767, after execute the above command, the value of usInputBit will be greater than 2^16.
Why dont you like to declare the temporary variables in input(), output() as "unsigned int"? You only re-declare they as "unsigned int" mean while the prototype of input(unsigned short), output(unsigned short) still has no change.
-
April 11th, 2004, 08:25 PM
#6
What do you mean, I should use unsigned int for usinput. usInput is the input data and they are given as unsigned short. So if I change it to unsigned int, how do I make sure the input value is not greater than 16b it. So assume that I typecast it which I haven't try yet. Will that solve the problem?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|