• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Shift

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Plz explain me how to find answer for Q1

Q1.
int x = -5;
for(int i=0;i<=31;i++)
{ x^=1<<i;x++;}

Q2.correct me if iam wrong

int a = -8;
int b = -33;
a>>>=b;
System.out.print(a);
is that the procedure ?
-33%32 = -1
-1%-8 = -1

My Answer : -1;

Thanks




}
}
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


this is a for loop that happens 32 times. But, I'm going to explain
the first iteration and you should be able to work thru yourself for
reminder of the iterations.

for the first iteration, i = 0 and the block is evaluated as

x ^ = 1 << 0

this becomes

x = x ^ ( 1 << 0)

= x ^ 1 (remember 1 << 0 = 1, as we are shift 0 bits)

= -5 ^ 1
= 1111 1111 1111 1111 1111 1111 1111 1011 ( ==> -5)
^
0000 0000 0000 0000 0000 0000 0000 0001 ( ==> 1)
-------------------------------------------
1111 1111 1111 1111 1111 1111 1111 1010 ( ==> -6)

so for the first iteration the result of x is -6

the next iteration is evaluated as

x = x ^ 1 << i (on the second iteration i = 1 and x = -6 from
the above step)

= -6 ^ ( 1 << 1)

and so on
 
raghu babu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


To know whether your answer is correct or not, I would
compile the code and run it. If you did so, you would have
noticed answer to the above question is not -1 but 1.

When the shift distance (i.e., right handside operand of the
any shift operator) is greaer than 31 and less than 0 then
we have to mask the right hand operand with a value of 31
(for integer type variables).

This means, in this case, the right hand operand is -1 so
apply the following operation to get the positive shift distance

-1 & 31

Hope this gives a clue, to solve the rest of the problem.
 
kavin kumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks babu,

Iam fine with the 1st question.

But still iam not clear with 2nd Question.
int = 32 bits right then how come u mask with 31.

And is there any simple tactics to find >>> or >>>= value

like x*2pow(2)(num of bits shifted) = x<<2
x/2pow(2) = x>>2

Thanks in advance
 
raghu babu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There can be multiple explanations, but the simple logic is if you move more than 31 bits, say 32 bits, then bits fall off as int can support only 32-bits, i.e., at the most you can shift only 31-bits.
 
author
Posts: 9000
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just making sure everyone knows this topic IS on the 1.4 exam... but if you're studying for 1.5...

you can skip it
 
When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic