2

Can anybody please explain what is happening below;how does the following bit shift works?

Dim pBuffer(11) As Int 
                pBuffer(0)=4 
                'Firmware Version'
                pBuffer(1)=Bit.ShiftRight(Bit.And(firmware_version, 0xFF00),8)
                pBuffer(2)=Bit.And(firmware_version, 0xFF)

Consider firmware_version = 0001

What will be the equivalent java code for this? Can I use Bit.ShiftRight in java? I checked the Java classes and didnt quite understand what will be the equivalent 'Bit.ShiftRight' operation in java?

EDIT: Can you explain what exactly is happening here?

pBuffer(1)=Bit.ShiftRight(Bit.And(firmware_version, 0xFF00),8)

How is the AND performed and the bit shift? What is it performed on? Is firmware_version AND 0xFF00 or is it bit shift first? Can you please explain?

Sarah
  • 1,895
  • 2
  • 21
  • 39
  • possible duplicate of [vb.net code for Android app](http://stackoverflow.com/questions/13798383/vb-net-code-for-android-app) <-- This one already covers you asking what `ShiftRight` and `And` do – Esailija Dec 16 '12 at 12:32
  • @Esailija I don't think that makes it a duplicate though. – Anirudh Ramanathan Dec 16 '12 at 12:34
  • @Cthulhu what is the difference then, the answers here misunderstood the real intent of the question, which is to explain how the bitwise operations work, and that is already covered by the question I linked, with the same OP asking the same questions. – Esailija Dec 16 '12 at 12:37
  • @Esailija the previous question had a lengthier code and I still had doubts. In this question I posted the exact line of code that was boggling my head. SO is quite helpful with concise and to the point answers. – Sarah Dec 16 '12 at 12:41

2 Answers2

5

in java, you can use the operators >> and >>> for signed and zero-filled right shift, respectively.

Bit-wise and is achieved in Java by &.

See http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

The VB statement

pBuffer(1)=Bit.ShiftRight(Bit.And(firmware_version, 0xFF00),8)

Can therefore by achieved in Java like

pBuffer[1]= (firmware_version & 0xFF00) // Bit.And
            >> 8                        // shift right 8 bits
gefei
  • 18,922
  • 9
  • 50
  • 67
  • Or `>>>` depending on whether it is signed or unsigned. – Peter Lawrey Dec 16 '12 at 12:19
  • Can you explain what exactly is happening here?pBuffer(1)=Bit.ShiftRight(Bit.And(firmware_version, 0xFF00),8) How is the AND performed and the bit shift? What is it performed on? Is firmware_version AND 0xFF00 or is it bit shift first? Can you please explain? – Sarah Dec 16 '12 at 12:25
  • 1
    @Sarah `Anding` first, then `bit-shift`, on the result of the `anding`. – Anirudh Ramanathan Dec 16 '12 at 12:33
2

VB.NET: Bit.ShiftRight(Bit.And(firmware_version, 0xFF00),8)

(Bitwise-and of firmware_version and 0xFF00 is shifted right 8 times)

Java  : (firmware_version & 0xFF00) >> 8 //or >>> for unsigned right-shift

VB.NET: Bit.And(firmware_version, 0xFF)

Bitwise And of firmware_version with 0xFF

Java  : (firmware_version & 0xFF)

The & operator in Java performs a bitwise AND operation.

(Reference)

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
  • The 'firmware_version' can be an int value? The equivalent java code that you posted in doing the AND between int AND 0xFF? Or do I have to convert the int firmware_version to binary or bytes? – Sarah Dec 16 '12 at 12:36
  • 1
    @Sarah Yes it can be an integer value. The `anding` and shifts are done on the bitwise representation of it. See the example at the bottom of the reference page. – Anirudh Ramanathan Dec 16 '12 at 12:37