Skip to main content

Good tutorials on Bitwise operations in Java


Can anyone please point me to good online tutorials on Bitwise Operations (preferably in Java)? Before anyone write LMGTFY, I've already Googled it but couldn't find one with good examples. Java's own tutorial is also not that extensive and doesn't clearly explain what is going under the hood. I'm a visual learner; so, if there are any resources with good 'working' examples, that would help me a lot. Thanks.



Source: Tips4all

Comments

  1. Vipan Singla's page on Bitwise Operations is quite nice.

    It is not language specific, but luckily, these operations work the same on Java as many languages (in particular, nearly all C-style languages work this way with bitwise operations). It is very visual, and uses tables to illustrate what happens by operation.

    ReplyDelete
  2. Additionally to the good introductory guide that Reed Copsey recommended I'd recommend a intresting collection of small real world samples that show what you can actually do with bit operators and that you might enjoy reading and experimenting with once you have got some basic understanding of how bitwise operators work.

    Regarding the differences of bitwise operators between Java and C/C++: In Java you don't have unsigned numeric types (except char) but instead you have two right shift operators (>> and >>>) that allow you to do either a signed (>>) or a unsigned shift (>>>). A signed right shift copies the sign bit to all empty places on the left side while a unsigned right shift fills them with zero bits. Also, as booleans in Java cannot be be cast to ints, you cannot use boolean expressions directly as operands for bit operators. I.e. something like this works in C but not in Java:


    val |= ( a > b );


    In Java you would have to write this insead to do the same thing:


    val |= ( a > b ) ? 1 : 0;

    ReplyDelete
  3. What's wrong with the WIkipedia article?

    ReplyDelete
  4. A perennial favorite: Sean Eron Anderson's Bit Twiddling Hacks.

    ReplyDelete
  5. As an introduction to bitwise operations to represent sets and efficiently compute subsets I liked this tutorial. It contains links to samples, including Java code (if you keep following the links at the bottom of the linked pages).

    ReplyDelete
  6. Also check Hacker's Delight, there is a sample chapter which is available online. Though not specific to Java, the programming tricks explained here would be applicable with Java also.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.