aspose file tools
The moose likes Beginning Java and the fly likes what does these bitwise shift doing here ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "what does these bitwise shift doing here ??" Watch "what does these bitwise shift doing here ??" New topic
Author

what does these bitwise shift doing here ??

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 304
Hello Everybody,
I was trying some code given n net for reading BMP file and saving into another format and not able to understand the following code which is converting int into Dword. My Int value is 64 which I am trying to convert.:-

I would like to request if someone can spare a moment and explain me following:-
1. Why I need to use >> ?
2. How one can decide how much shift is needed and in which direcion?
3. Why there is a need of using & operator ?
4. How one can decide with which it should be done as it is done with 0x000000FF for 2,3 and 4 th element.
I would be highly obliged if some one can help me to understand the above.
regrds,
arun
Neil Laurance
Ranch Hand

Joined: Jul 18, 2002
Posts: 183

This can be best explained with an example.
Suppose parValue has the bit pattern (split into 8 bit segments for readability:
11111111 00000000 11110000 00001111
Now, the value 0x00FF is the bit mask:
00000000 00000000 00000000 11111111
So, for retValue[0] we simply AND these patterns together:
11111111 00000000 11110000 00001111 &
00000000 00000000 00000000 11111111
-----------------------------------
00000000 00000000 00000000 00001111
and then recast to byte to become:
00001111
For retValue[1] we use a signed right shift of 8 positions:
11111111 00000000 11110000 00001111 >> 8
-----------------------------------
11111111 11111111 00000000 11110000
Then we use the bit mask to get the 8 rightmost bits again:
11111111 11111111 00000000 11110000 &
00000000 00000000 00000000 11111111
-----------------------------------
00000000 00000000 00000000 11110000
And then recast to give the byte value 11110000
etc.. etc..
[ July 26, 2002: Message edited by: Neil Laurance ]
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 304
Thanks Neil for your answer..but I still have some questions on it:-
1. Why I need to use >> ? why not << ?
2. How one can decide how much shift is needed and in which direcion?
3. Why there is a need of using & operator ? Why this operator why not | .
4. Why it is putting & with 0xooff for first element and why 0x000000FF with 2,3 and 4 th element.
I could understand that this has something very standard for conversion..but is the principal and where can I get these specifications?
Thanks once again for your reply.
regards,
arun
Stu Glassman
Ranch Hand

Joined: Jul 01, 2002
Posts: 91
Ok, an int has 4 bytes, which looks something like this in hexadecimal:
0x???
The method is trying to break the int into a four element byte array. If the int was this:
0x12345678
Then the byte array should be:
retValue[0] = 0x78
retValue[1] = 0x56
retValue[2] = 0x34
retValue[3] = 0x12
Here's how this is done:
1. Chop off the end of the int and store it in the array
In order to accomplish this, we use a bitstring. In this case, 0x000000ff. We combine the int and the bitstring using the & (AND) operator. The & operator performs the following operation on each bit:
If both operands are 1, set the answer to 1. Otherwise, set the answer to 0.
The bitstring 0x000000ff looks like this in binary:
00000000 00000000 00000000 11111111
So, the answer will have the left three bytes as all 0, and the right byte will be whatever the right byte of the other operand is. In other words, if we do number & 0x000000ff, the left three bytes of number will be effectivly trimmed off. For example:
0x12345678 & 0x000000ff = 0x00000078
If we had used the or operator (|), then the expression would have looked like this:
0x12345678 | 0x000000ff = 0x123456ff
Which is not what we want.
2. Shift the int to the right by one byte
Basically, what we want to do is change 0x12345678 into 0x00123456. Java provides nifty bitshifting operators to do this (<<, >> ;) . Since we want to shift to the right, we use the right shift operator, >>. We want to shift by one byte, so we do this:
0x12345678 >> 1
And get an answer of 0x00123456
3. Go back to step one until the entire number is finished
That was the algorithm used. Let's see about your questions:
1. Why I need to use >> ?
It's the right shift operator (see next question)
2. How one can decide how much shift is needed and in which direcion?
Depends on the problem. Technically, you could have used the bitstring 0xff000000 and shifted to the left. Or, you could have used four different bitstrings (0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff) and not shifted at all.
3. Why there is a need of using & operator ?
Actually, you probably could have just cast it to a byte. However, using the & operator to trim the number down to the least significant byte is much better coding style.
4. Why it is putting & with 0xooff for first element and why 0x000000FF with 2,3 and 4 th element.
First of all, that's not 0xooff, it's really 0x00ff.
In math, the number 005 is the same as 5. You can stick as many zeros in front of a number as you'd like. So, 0x00ff is exactly the same as 0x000000ff.
Hope this helps,
-Stu
Edited - fixed a script tag
[ July 26, 2002: Message edited by: Stu Glassman ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what does these bitwise shift doing here ??
 
Similar Threads
Java endian and bit order and signed and unsigned and python and more confusion than I can deal with
JavaPOS - send packet to device..
Sending header info in hex format
unsigned integer
converting C++ type struct to Java class?