• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Bitwise Operators

 
Ranch Hand
Posts: 78
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reading through bitwise operators and I feel like I'm walking through deep mud and my shoes keep coming off. I understand it but I'm wondering at the same time if it's anything I'll ever use.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://stackoverflow.com/questions/2096916/real-world-use-cases-of-bitwise-operators
 
Brian Barrick
Ranch Hand
Posts: 78
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I'll be sure and revisit it in the future. Right now I don't think I want to get too deep into it though.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Beware: some of the code in that link will only work in C/C++.
 
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

Brian Barrick wrote:Thanks, I'll be sure and revisit it in the future. Right now I don't think I want to get too deep into it though.


Probably quite wise. Bitwise ops tend to be used for optimizing, which (as I hope you know) is the root of all evil.

That said, they can be useful (and interesting, in a geeky way) in the right circumstances.

Winston
 
Brian Barrick
Ranch Hand
Posts: 78
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was a bit surprised that it was in a beginners book fairly early on, right after explaining string objects. I could see it being touched on near the end but it seems more like an intermediate or even an advanced topic that most beginners wouldn't care about.
 
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

Winston Gutkowski wrote:... optimizing, which (as I hope you know) is the root of all evil.


That's not exactly right... the famous quote from prof. Knuth is: "premature optimization is the root of all evil". Not just optimization in general.

The point of the quote is: don't waste time optimizing your code until you have proof that there is a performance problem. Optimization is one of those things where programmers are prone to do magical tricks because they have some vague feeling that those tricks will make code perform better. In reality, the only right way to optimize is to take the scientific approach: measure (don't guess) to find out what part of the program actually causes a problem, then try to improve that part, and repeat. Your vague feelings (intuition) is most often mistaken when it comes to performance optimization.
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did once find myself writing an if statement of the form:



which can be 'simplified' to



which is the only time I can remember using a bitwise operator when I wasn't doing a low level operation.

Personally I preferred the shorter form because I find it easier to read, but I imagine others may not like it.
 
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

Jesper de Jong wrote:That's not exactly right... the famous quote from prof. Knuth is: "premature optimization is the root of all evil". Not just optimization in general.


True. My slant on the old chestnut, but I stick by it.

Optimization (if needed) should almost always be a design choice; not a coding one - and the choice of using bitwise ops is definitely a coding one.
In thirty-plus years, I've yet to see a choice like that make a scrap of difference in overall performance; but I've seen LOTS of cases where it caused problems.

Case in point: the "XOR swap" - a wonderful bit of bitwise magic that is great as a teaching tool, but about as useful as a recursive factorial method when it comes to optimisation.

@Brian: We're arguing esoterics here (as we often do ). My advice to you: I think your instincts are right.

Learn how to do what you need to "the straightforward way" first. Then, if you need to (or if it interests you), find out what bitwise ops can do. They ARE blisteringly fast; but they can also be very arcane.

Winston
 
If you believe you can tell me what to think, I believe I can tell you where to go. Go read this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic