Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

# LRS on multiple variables, is it possible in java ?

naved momin
Ranch Hand
Posts: 692
i guess most of the people here knows about LRS (Logical Right Shift ...inshort '>>>' operator of java) so i m asking my doubt directly
c, ac and q are the three registers

c ac q msg
========================
0 0000 0010 initial value
------------------------------------------
0 0000 0001 LRS
------------------------------------------

what i did is simple LRS , i was wondering is there a way to perform LRS on 2 variables simultaneously i.e ac and q resp.
because if i do LRS in individual variables then it is ok for the above senerio but it creates problem below

c ac q msg
============================
0 0000 0010 initial value 4)
---------------------------------------------------
0 0000 0001 LRS 3)
---------------------------------------------------
0 0001 0001 somehow we get this 2)
---------------------------------------------------
0 0000 1000 LRS 1)
---------------------------------------------------

i guess now the picture is clear for you, if we do LRS on single variable then it will give me ac = 0 & c = 0 in 1)... but here we are getting ac = 0 & q = (8)base10

Henry Wong
author
Marshal
Posts: 21114
78
naved momin wrote:i guess most of the people here knows about LRS (Logical Right Shift ...inshort '>>>' operator of java) so i m asking my doubt directly
c, ac and q are the three registers

c ac q msg
========================
0 0000 0010 initial value
------------------------------------------
0 0000 0001 LRS
------------------------------------------

what i did is simple LRS , i was wondering is there a way to perform LRS on 2 variables simultaneously i.e ac and q resp.
because if i do LRS in individual variables then it is ok for the above senerio but it creates problem below

c ac q msg
============================
0 0000 0010 initial value 4)
---------------------------------------------------
0 0000 0001 LRS 3)
---------------------------------------------------
0 0001 0001 somehow we get this 2)
---------------------------------------------------
0 0000 1000 LRS 1)
---------------------------------------------------

i guess now the picture is clear for you, if we do LRS on single variable then it will give me ac = 0 & c = 0 in 1)... but here we are getting ac = 0 & q = (8)base10

Sorry.... but I read your question three times and have no idea what you are asking. Can you (or anybody) elaborate???

Henry

Jeff Verdegan
Bartender
Posts: 6109
6
I too an unclear on what you're asking. I have two guesses as to what you might mean by shifting "2 variables simultaneously."

1. You want two operations to happen simultaneously in time, that is, in parallel. Use multiple threads. There's no way to force the operations to truly happen simultaneously in time, but as far as your code is concerned, they are happening concurrently.

2. You want to connect two variables back-to-front so that what's shifted out of one goes into the other. If they're ints or smaller, you can combine them into the next largest size, shift that one, and then split the result. Otherwise you'll have to do it piecemeal.

Henry Wong
author
Marshal
Posts: 21114
78
Jeff Verdegan wrote:
2. You want to connect two variables back-to-front so that what's shifted out of one goes into the other. If they're ints or smaller, you can combine them into the next largest size, shift that one, and then split the result. Otherwise you'll have to do it piecemeal.

Many processors have a "roll" instruction -- or in this case, a "roll right with carry" instruction (or at least, it existed when I coded in assembly two decades ago). This instruction shifts the word towards the right, putting the lowest bit into the carry, while putting the previous carry value into the highest bit.

Unfortunately, Java doesn't have this instruction, so you will need to use a combination of bitwise ANDs, ORs, and if-then statements to achieve this.

Henry

naved momin
Ranch Hand
Posts: 692
Jeff Verdegan wrote:

2. You want to connect two variables back-to-front so that what's shifted out of one goes into the other.

yes, i want to achieve this but in Java we can perform ARS or LRS on decimal numbers and not on binary numbers...what i mean is ...you were telling me to combine or add to operands and then do ARS or LRS but this will yield me result which is not similar to LRS done with pen and paper

naved momin
Ranch Hand
Posts: 692
Henry Wong wrote:

Unfortunately, Java doesn't have this instruction, so you will need to use a combination of bitwise ANDs, ORs, and if-then statements to achieve this.

Henry

yes, i already written the solution just wanted to know whether such a trick exists or not ?

Jeff Verdegan
Bartender
Posts: 6109
6
naved momin wrote:
Jeff Verdegan wrote:

2. You want to connect two variables back-to-front so that what's shifted out of one goes into the other.

yes, i want to achieve this but in Java we can perform ARS or LRS on decimal numbers and not on binary numbers...what i mean is ...you were telling me to combine or add to operands and then do ARS or LRS but this will yield me result which is not similar to LRS done with pen and paper

Again, I don't understand what you're saying. What do you mean "decimal numbers, not binary numbers"?

produces exactly the output expected:

naved momin
Ranch Hand
Posts: 692
Jeff Verdegan wrote:

produces exactly the output expected:

Jeff my frnd again you have not understood what i mean
when you or i do this i2 >> 32 //we are writing 65536 (i2) as a decimal no. agreed ? Java is the one which converts it into binary and perform the ARS
but we cannot do something like this
after this i hope picture wil be more clear
let a , b & c be any 3 variables

=====================================
C---------A----------Q---------------message
0 ------ 0001 ---- 0011 --------- initial value
|----------------------------------------------------------------
0 ------- 0000 ----- 1001 --------- ARS
-----------------------------------------------------------------

so let me ask you one question , can you perform ARS so that "1" is shifted from "A" to "Q" which makes content of Q to "1001" using just Java's ARS operator ">>" ?

Jeff Verdegan
Bartender
Posts: 6109
6
naved momin wrote:
but we cannot do something like this

We can, but it won't have the value of 3, because Java doesn't let you represent ints using binary literals that way.

Your question has nothing to do with shifting and is only about how to represent values in base-2 in your code.

First, it's important that you understand that all of the following generate exactly the same values, and there's no way to tell which way the value came about.

As for representing 3 as 11, there's no binary literal in Java as far as I know, so you'd have to do Integer.parseInt("11", 2) (or ("0011", 2) would work also, I tihnk).

so let me ask you one question , can you perform ARS so that "1" is shifted from "A" to "Q" which makes content of Q to "1001" using just Java's ARS operator ">>" ?

You can perform a shift on any int, regardless of how you got the value, and the result will always be the same. Just make sure you understand that 11 is a different value from 011 which is different from Integer.parseInt("11", 2), and make sure you understand what each of those expressions means, and why.

Again, the issue here has nothing to do with shifting, and is only about how to express the desired integer in the first place.

Rameshwar Soni
Ranch Hand
Posts: 247
Jeff Verdegan wrote:
there's no binary literal in Java as far as I know

But if you are using Java 7 thats possible, here

naved momin
Ranch Hand
Posts: 692
Jeff Verdegan wrote:
naved momin wrote:
but we cannot do something like this

We can, but it won't have the value of 3

yes, i know we can but answer will be wrong for binary 0011

Jeff Verdegan wrote:
First, it's important that you understand that all of the following generate exactly the same values,

I guess, i am not able to make you understand what i want , because i know that you write "0011" or "11" in base 2 both will give me answer as 3 in decimal. and i do have knowledge about base 2,5,8,16
so thanks every one and jeff for explaining things, but i some how fail to explain you my problem ...but its ok i have implemented the solution.

Jeff Verdegan
Bartender
Posts: 6109
6
naved momin wrote:
Jeff Verdegan wrote:
naved momin wrote:
but we cannot do something like this

We can, but it won't have the value of 3

yes, i know we can but answer will be wrong for binary 0011

It won't be wrong. Java just doesn't support binary notation before version 7. The result will be correct, as per the JLS. Just because it's not what you expected, that doesn't mean it's wrong.

i know that you write "0011" or "11" in base 2 both will give me answer as 3 in decimal.

Not in Java, no. Java has specific rules about how literals in your code get turned into values. Those rules may not follow your expectations, but that doesn't mean Java is wrong, only that your expectations are wrong. (However, as somebody else linked, starting with Java 7, you can use 0b0011.)

You can shift the binay value 11, which is 3 in base 10. You just can't write it as a binary literal before Java 7. But if you're using Java 7, you can do this:

i1 and i2 are exactly the same. i3 and i4 are exactly the same.

but i some how fail to explain you my problem

Yes, I really don't understand what problem or question you still have.

It may help to make your question clearer if you provide an SSCCE(←click) that comes as close to doing what you want as you can manage. Put some hardcoded data in it, and indicate what the actual output is and what output you want to achieve instead.