Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java leftshift operator add 1 instead of 0

 
J amodi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20995
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
J amodi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Henry Wong
author
Marshal
Pie
Posts: 20995
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 10226
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 20995
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10226
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20995
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic