• Post Reply Bookmark Topic Watch Topic
  • New Topic

Bitwise Operators  RSS feed

 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • 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.
 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware: some of the code in that link will only work in C/C++.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 67
Android Java
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • 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: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!