• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Java leftshift operator add 1 instead of 0

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 75851
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 75851
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic