The Artima Developer Community
Sponsored Link

Java Answers Forum
parsing a short into 2 pieces

7 replies on 1 page. Most recent reply: Nov 13, 2003 1:19 AM by Senthoorkumaran Punniamoorthy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
Tanya

Posts: 16
Nickname: seabeck
Registered: Nov, 2003

parsing a short into 2 pieces Posted: Nov 1, 2003 5:56 PM
Reply to this message Reply
Advertisement
I have an array of shorts and I need to parse the value in one of the fields into 2 parts. For instance shtVal[2] = 20. I need to get the 2 and then I need to get the 0 by themselves. I created a method that does the first part by using int val = shtVal[2];
val <<= 8;

This seems to work correctly but when I try to shift it right by doing
val >>= 8; It gives me the incorect value.

Thanks for any help you may provide.


Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: parsing a short into 2 pieces Posted: Nov 1, 2003 5:59 PM
Reply to this message Reply
Why don't you post your code what you have now?

Tanya

Posts: 16
Nickname: seabeck
Registered: Nov, 2003

Re: parsing a short into 2 pieces Posted: Nov 1, 2003 6:04 PM
Reply to this message Reply
Here is my code I am simulating computer memory IR stands for instruction register. We are using hex values for our instructions.

private int getR1() //this is for getting the first value
{
int r1 = reg.getIR(1);
r1 <<= 8;
r1 = (r1 / 0x1000);//convert hex value to int.
return r1;
}

private int getR2()
{
int r2 = reg.getIR(1);
r2 >>= 8;
//r2 = (r2 / 0x1000);
return r2;
}

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: parsing a short into 2 pieces Posted: Nov 1, 2003 6:23 PM
Reply to this message Reply
> I created a method that does the first part
> by using int val = shtVal[2];
> val <<= 8;

Sorry to ask this again. I am little confuced with your problem. say you have an int array

int test[] ={10,20,30};

then your code is suppose to take these values and produce and output.

Please explain me what the output should look like in detail. What I understand now is if you have the first value 10 basically you want to separate it into 1 and 0 and the next 20 to 2 and 0. Am I right? Please correct me if I am wrong?

Tanya

Posts: 16
Nickname: seabeck
Registered: Nov, 2003

Re: parsing a short into 2 pieces Posted: Nov 1, 2003 9:06 PM
Reply to this message Reply
Yes what I have now is an array of shorts that contain hexcidecimal values.
I am simulating a simple computer and I am loading an .obj file to the memory of the simple computer.
I have to use those opcodes to do simple tasks.

The array represents a 4 part instruction register and the second index contains a register to pass things to and a register to get things from.
For example I have int temp = IR[1]; temp might have a hexadecimal value of 21 and what I need to do is seperate the 2 and the 1.

I have to remember that they are hexadecimal rather than decimal values. That is why in the first method I had to / by 0x1000. I don't understand why this works but it does.

what I am having problems with is that I need to get the 1. and I tried to do the same but with reversed >> operands. What I always get is 0 for the answer which is the wrong answer.

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: parsing a short into 2 pieces Posted: Nov 2, 2003 7:57 PM
Reply to this message Reply
WHy don't you use / and % operaters to get the same results?
    System.out.println(0x34);
        System.out.println(0x34 / 16);
        System.out.println(0x34 % 16);


Wouldn't that help you?

Tanya

Posts: 16
Nickname: seabeck
Registered: Nov, 2003

Re: parsing a short into 2 pieces Posted: Nov 12, 2003 11:48 PM
Reply to this message Reply
Thank you for your help, I actually used a slightly different version that rather than dividing by 16 I shift the bit by 4 which is the same thing because it basically is 2^4 = 16. Here is my final code that I used.

private int getR1()
{
      int r1=((reg.getIR(1) &0xF0));
      r1 >>= 4;
      return r1;
}
 
private int getR2()
{
      int r2= reg.getIR(1);
      r2 = r2&0xF;
      return r2;
}

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: parsing a short into 2 pieces Posted: Nov 13, 2003 1:19 AM
Reply to this message Reply
Thanks for letting us know the results

Flat View: This topic has 7 replies on 1 page
Topic: Example of JSP code Previous Topic   Next Topic Topic: help me

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use