?
Solved

Bitwise Copy

Posted on 2000-05-15
4
Medium Priority
?
271 Views
Last Modified: 2006-11-17
Hi,
I have an int i=0xffffffff which I want to copy to a long l , so that the result is l=0x00000000ffffffff; , Can you pls tell me how to do it.

thanks in advance
regards
mak
0
Comment
Question by:makrand
  • 2
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 400 total points
ID: 2811699
There should be no special handling required:

int i=0xffffffff;
long l=i;

Since l is a "wider" type than i, it can be assigned without any trouble at all.
0
 

Author Comment

by:makrand
ID: 2811903
When u check the binary value of it using
Long.toBinaryString(longVal) and
Integer.toBinaryString(intVal), you will notice the diffrence
0
 
LVL 16

Expert Comment

by:imladris
ID: 2812385
Yes, you are quite right. I apologize. The reason for the behaviour you see is that integers (and longs) are signed quantites. The value 0xffffffff represents -1 in an integer. When it is converted to a long, the sign bit is extended leftward (i.e. if the topmost bit is 0 the top 4 bytes of the long are set to 0, but if the top bit is 1 the top 4 bytes of the long are set to 1). Thus the long becomes 0xffffffffffffffff which also represents -1.

To force the long to contain 0xffffffff you will need to mask the top 4 bytes to 0:

long l=i&0xffffffffL;

Make sure you add the L to the constant which will cause the constant to be long (i.e. 8 bytes). This will cause the variable i to be widened (and sign extended) (in order to do the bitwise operation), then top 4 bytes will by masked off.

Without the trailing L, the bitwise operation would occur first (having no effect) and then the result would be widened (leaving you with the sign extension again.
0
 

Author Comment

by:makrand
ID: 2818431
Thanks very much, I was not getting the exact way to do a bitwise operation, I was doing something like this

private static long getBinaryLongValue(int val)
{
String s = Integer.toBinaryString(val);
long l = Long.parseLong(s,2);
return l;  
             
}
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to implement Singleton Design Pattern in Java.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month17 days, 10 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question