|
-
Oct 23rd, 2009, 03:10 PM
#1
Thread Starter
New Member
Binary In Java
Hi, I'm new here, and I have one doozy of a code.
Basically It involves binary, which I have little knowledge on how to do in java. It's a very roundabout code, but basically :
a hypothetical multi-touch device encodes information in 64-bit long integers. The values of up to 3 contact points -- (x1,y1), (x2,y2), (x3,y3) -- are recorded as well as the corresponding pressures -- p1, p2, p3.
The number of points reported, n, will be between 0 and 3 and is represented with 2 bits. The touch pad is wider than it is tall, so the x values are reported with 9 bits to give values between 0 and 511. The y values are reported with 7 bits to give values between 0 and 255. Pressure values are reported with 3 bits to give values between 0 and 7.
So this is a 2-parter, the first being a relatively simple CONCEPT. I must
A)Write a method that takes integers, representing x, y and p, and make a 64bit long integer. I’ll have 3 different methods, one for 1 contact point, 1 for 2 contact points, and 1 for 3 contact points. That way I’ll know what automatically what value to use for n.
B) Given a very 64bit integer, I’ll have a code that’ll ask for any of the x1, y1, x2, y2, x3 or y3, and I must supply them with it.
It’s pretty much the concept of the binary that’s throwing me off, really.
-
Oct 23rd, 2009, 08:08 PM
#2
Re: Binary In Java
So. What's your question?
Hopefully its not "Would you do my homework for me?"
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Oct 24th, 2009, 09:33 AM
#3
Thread Starter
New Member
Re: Binary In Java
No, I wouldn't expect anyone to do that for me, this is my work,I just need a little push in the right direction.
The long is 64 bits.
bits 0- 1, how many contact points.
bits 2- 3, unused
bits 4-12, x1
bits 13-20, y1
bits 21-29, x2
bits 30-37, y2
bits 38-46, x3
bits 47-54, y3
bits 55-57, p1
bits 58-60, p2
bits 61-63, p3.
Right now I'm figuring out how to do
public static long makeStatusWord(int x1, int y1, int p1), where this method only has one contact point. I have to turn the 3 integers I've gotten into one 'long', along with the first two bits saying that only 1 contact point was made. The rest should all come out as 0's.
That is, I BELIEVE that long is binary.
I guess I'm mostly confused about 'masks'
EDIT: I've already figured out how to take each part and move it to the right spot, using the << method
So something like L = n & x<< 4 & y<<13 & p<< 55.
Last edited by GiantEnemyCrab; Oct 24th, 2009 at 10:15 AM.
-
Oct 24th, 2009, 11:15 AM
#4
Re: Binary In Java
Still no question. Anyway your code should look something like
Code:
public static void main(final String[] args) {
final int x1 = 12, x2 = 12, x3 = 12, y1 = 12, y2 = 12, y3 = 12, p1 = 7, p2 = 7, p3 = 7;
long value;
value = (((long) p3 << 61) & 0xFFFFFFFFFFFFFFFFL);
value = (value | ((long) p2 << 58) & 0xFFFFFFFFFFFFFFFFL);
value = (value | ((long) p1 << 55) & 0xFFFFFFFFFFFFFFFFL);
value = (value | ((long) y3 << 47) & 0xFFFFFFFFFFFFFFFFL);
value = (value | ((long) x3 << 38) & 0xFFFFFFFFFFFFFFFFL);
value = (value | ((long) y2 << 30) & 0xFFFFFFFFFFFFFFFFL);
value = (value | ((long) x2 << 21) & 0xFFFFFFFFFFFFFFFFL);
value = (value | ((long) y1 << 13) & 0xFFFFFFFFFFFFFFFFL);
value = (value | ((long) x1 << 4) & 0xFFFFFFFFFFFFFFFFL);
value = (value & 0xFFFFFFFFFFFFFFF3L); // Set the unused bits to 0s
value = value | 3L;
System.out.println(Long.toHexString(value));
}
And if you want to learn more about bitwise operations visit http://www.java2s.com/Tutorial/Java/...-Operators.htm
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Oct 24th, 2009, 12:54 PM
#5
Thread Starter
New Member
Re: Binary In Java
Wow, the site's really helpful, and I guessed I should have posted I figured out that part afterwards, though seeing that shows I need one or two adjustments.....
Well, here's a completely legit question. For the next part, we must take out parts from a long and return an int. For example, it wants the integer value of x2. I know how to extract it, I just forgot how to turn it from long to int.
Code:
public static int numberOfPoints(long statusWord){
int n = statusWord & 0x2; //type mismatch error
return n;
}
EDIT: Nvm, I seem to be too quick in jumping to asking people. I got it.
Code:
public static int numberOfPoints(long statusWord){
long n = statusWord & 0x2;
int nint = (int)n;
return nint;
}
-
Oct 24th, 2009, 01:28 PM
#6
Re: Binary In Java
Good for you my friend. You're always welcome to ask.
If you're done here please don't forget to mark the thread resolved from the Thread tools menu
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Oct 24th, 2009, 01:44 PM
#7
Thread Starter
New Member
Re: Binary In Java
There's a second part of it which I've yet to do, so it's not done completely.
Also, I'm relooking at your code you posted
value = (value & 0xFFFFFFFFFFFFFFF3L); // Set the unused bits to 0s
value = value | 3L;
I don't get why the 3 is there..... from what i count, that makes 75.......
-
Oct 24th, 2009, 07:04 PM
#8
Re: Binary In Java
the first 3 is set to clear bits 2-3 (unused) and the second one is.. Well I assumed you'll always have 3 contact points
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
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
|