aspose file tools*
The moose likes Java in General and the fly likes Java leftshift operator add 1 instead of 0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java leftshift operator add 1 instead of 0" Watch "Java leftshift operator add 1 instead of 0" New topic
Author

Java leftshift operator add 1 instead of 0

J amodi
Greenhorn

Joined: Nov 09, 2011
Posts: 18
Hi Guys,

I have a task to do. If I have some some int variable and If I apply left shift operator on that, it adds "0" on left side of value.

For example,

So, it will work like this,
5 as binary : 0101
1st time left shift: 01010
2nd time left shift: 010100

So, the answer will be 20.

But, I want to have "1" instead of "0"s.
So, it should have something like this..:


5 as binary : 0101
1st time left shift: 01011
2nd time left shift: 010111

So, the value should be 23.

How do I do this in java. Please help me asap.

Thanks.
Code Eater.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40


Java doesn't have an operator that directly does this -- however, you can easily achieve this with a combination of the left shift and some of the bit wise operators.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
J amodi
Greenhorn

Joined: Nov 09, 2011
Posts: 18
Hi Henry,

Could you give me a example? and about other languages, is this possible using any other language ? with C language ?

Thanks.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

J amodi wrote:Hi Henry,

Could you give me a example?


Just shift left and then bitwise OR with 1.


and about other languages, is this possible using any other language ? with C language ?


No, not in C or in any other language I've ever worked with. Why do you think you need this, and would you really base your choice of language on this feature?
J amodi
Greenhorn

Joined: Nov 09, 2011
Posts: 18
Hi Jeff,

Thanks for your reply. I need to decide the language based on this, because my entire code is based on this feature only.


Thanks.
Code eater
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

J amodi wrote:Thanks for your reply. I need to decide the language based on this, because my entire code is based on this feature only.

I don't understand this. Do you really want to choose a language because it could do this operation with one operator? Why? What would it gain you if you could write this with one operator instead of something like result = (x << 1) | 1? Do you think it would somehow be more efficient if you'd have a single operator for this? Well, it is not, because your CPU doesn't even have a single instruction for this operation, so a compiler for a hypothetical language that would have a single operator for this would compile it to multiple CPU instructions anyway. (Besides that, micro-optimisation like this doesn't make sense).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40


Having coded in assembly language, on a few processors, I know that I have never encountered a shift left that shifts in a value of one. Of course, I haven't coded with all processors, and I haven't coded with any new processors (as I haven't worked with assembly in years), so there may be a new processor that does this.

I do remember that you can do a rotate left for this -- processors have a rotate left with carry, so if the carry is one, you can rotate left (with the one value rotating into the lowest bit). Of course, you still need to have the extra step of setting the carry to one.


Henry

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7795
    
  21

J amodi wrote:Thanks for your reply. I need to decide the language based on this, because my entire code is based on this feature only.

Then I'd suggest there's a flaw in your design technique. Why would you base your entire code around an operation that you don't even know exists? And BTW, the solution you've been given ((x << 1) | 1) probably equates to less than 5 machine instructions (but, like Henry it's been a long time since I did assembler).

In theory, I suppose you could say that its effect on a value x is to create 2x+1, but only if there's no overflow; so I'm not quite sure why it's so important.

In case you're interested, you could also emulate the op for the case of n > 0 with:
(x << n) | (-1 >>> (32 - n))
which may seem like a mouthful, but is probably quicker than calling a single shift n times.


Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

Winston Gutkowski wrote:
In case you're interested, you could also emulate the op for the case of n > 0 with:
(x << n) | (-1 >>> (32 - n))
which may seem like a mouthful, but is probably quicker than calling a single shift n times.



I think I can do it with one less operator... .... try ...

~((~x) << n)


Henry
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4391
    
    8

Henry Wong wrote:I think I can do it with one less operator... .... try ...

~((~x) << n)

Is that a Java expression, or a particularly complicated emoticon?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Matthew Brown wrote: . . . a particularly complicated emoticon?
It’s the emoticon for “I’ve just found some unmaintainable code”.
I am tempted to report that to Roedy Green as an addition to his list. I am also tempted to move this discussion as too difficult for “beginning”.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7795
    
  21

Henry Wong wrote:I think I can do it with one less operator... .... try ...
~((~x) << n)

Cool. I like that. And it works for n == 0 too. Java hasn't bashed all the C out of me yet...

Winston
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

J amodi wrote:Hi Jeff,

Thanks for your reply. I need to decide the language based on this, because my entire code is based on this feature only.


This makes no sense. It says that the majority of the work your code does will simply be this operation, and further more, that you already know--because you have already measured--that the 2-step shift-or will be too slow for your specific, well--defined performance requirements.

This does not sound like a believable scenario to me.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Winston Gutkowski wrote:. . . Java hasn't bashed all the C out of me yet...

Winston
Yes, it has, otherwise you would have removed the redundant ()
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

Campbell Ritchie wrote:
Winston Gutkowski wrote:. . . Java hasn't bashed all the C out of me yet...

Winston
Yes, it has, otherwise you would have removed the redundant ()



I always seem to put extra parens in expressions. Too many years of programming between C/C++ and Java -- and dealing with how the precedence between them are slighly different.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java leftshift operator add 1 instead of 0