Solved

Bitwise Copy

Posted on 2000-05-15
4
228 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 100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 76
JavaScript/Java - Changing an image background color 4 54
xampp tool 12 23
tomcat not starting 6 24
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

929 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now