views:

671

answers:

8

I didn't know any better name for a title of the question.

For ex: on one of my previous questions one answered ((a)-(b))&0x80000000) >> 31 - this is kind of a too advanced for me and i can't really get what it means. I am not looking just for an answer of that, but i need someone to tell me some books/sites/whatever where i can learn this cool "advanced" tricks in C - and learn how and where to use them respectively too.

+5  A: 

That's not assembly, it's bitwise arithmetic (wiki). (or this tutorial)

As for when to use it... well, sometimes there are valid uses, but you'll probably already know you need it at that point. That previous answer you mention was given in response to a question where you asked for obfuscation, which ought to give you an idea how necessary it generally is.

Jefromi
I think what he means is that it just looks cryptic like assembly.
Fabio Ceconello
@Fabio: Yeah, I know, just always want to clear up any possible misunderstandings.
Jefromi
A: 

Try this link. If it doesn't explain it properly, try googling for "bitwise operation" "bitwise operator" and so on.

link text

ziggystar
+2  A: 

Take a look at Expert C Programming

http://www.amazon.com/Expert-Programming-Peter-van-Linden/dp/0131774298/ref=sr_1_1?ie=UTF8&s=books&qid=1268852658&sr=8-1

Fabio Ceconello
This is my favorite technical book of all time.
Andrew O'Reilly
A: 

I have found the following article to be a very clear explanation of bitwise operations -- http://www.gamedev.net/reference/articles/article1563.asp

jsumners
+6  A: 

For bitwise arithmetic, make sure you know binary. Forget the math or converting to and from base 10, but you should know how to deal with hex. (For instance, 00010001 should INSTANTLY flash 11 in your head (This is why people use hex, it's so easy to think in binary).

Note that on your question, the 0x80... number. 8 you should recognize as a power of 2 (1000), and all the other 0's are obviously 0.. so at a glance you should know that it's a long stream of 0's preceded by a single 1 in binary.

You should know how to and/or/not binary numbers and possibly hex at a glance.

0101 & 0011 = 0001, 0101 | 0011 = 0111 should be obvious.  

Slightly less obvious would be the same stuff written in hex:

0x5 & 0x3 = 0x1, 0x5 | 0x3 = 0x7

Also know about shifting. How shifting can double or half the value of a number easily, and how to shift a bitfield around to get what you want.

Work with this stuff for a few weeks and then try to start looking at more binary expressions.

Assembler is another beast altogether, but you'll need to understand binary arithmetic to even begin with assembler.

Edit: (Seeing that you liked my answer, I'll give some bonus content)

I love playing with binary. Try counting to 31 on one hand--count things on TV in binary on your fingers. Write out tables in base 2, 3, 4, 5, ...:

Base: 10    2     3     4     5    ... (Keep going)
      --  ---  ----  ----  ----
       1    1     1     1     1
       2   10     2     2     2
       3   11    10     3     3
       6  110    20    12    11
     ...

Understanding other bases can help you understand binary.

In fact, here's a fun exercise I gave a few people once... One guy actually got it. What's the next number in this sequence:

10, 11, 12, 13, 14, 15, 20, 22, 30, ?

(Since Lars figured it out, I'll put a few more hints in the comments--consider 'em spoilers)

Add, divide, subtract and multiply numbers in binary. Long division in binary works exactly like long division in base 10 but easier. Same with long-form multiplication. Adding a long string of large binary numbers is so easy it's seriously fun!

As you do this kind of operation, you'll start to get an insight into what the CPU does when it manipulates numbers. Realizing how much easier binary is to do than base 10 math should give you a real superiority complex (at least vs stupid CPUs that can't approach the complexity of base 10 and instead just does binary math REALLY FAST to compensate.)

Bill K
Well, what is the next number?
Lars
110, doesn't help much :) The NEXT one might though. If nobody gets it in the next few days I'll post the entire series (Nearly everything interesting is already up there though)
Bill K
+1. Excellent answer
Appu
I just found a "bug" in my sequence question above. It's actually a pretty hard sequence to get right. I'll wait another day and post the entire sequence. How about as an award If anyone figures it out before I post a full answer I'll go look through their posts and vote +1 on everything that doesn't look completely wrong...
Bill K
A-Ha! Now it makes more sense - the next number after 110 is 1100 (and the number preceding 10 is also known as a popular programming language).
Lars
You had some good answers, @Lars, it was a pleasure to vote them up :) Here's the entire sequence in the "correct" order: 1100, 30, 22, 20, 15, 14, 13, 12, 11, 10, c, c, c... (c repeats forever.) I won't leave the actual answer unless someone really cares--it's more fun to figure out yourself.
Bill K
\o/ (but the sequence above is now missing '110' in its second place :-) )
Lars
Damn, correct--and I can't edit it again. 1100, 110, 30, 22, 20, 15, 14, 13, 12, 11, 10, c (repeating). Hope I got it right this time. It's a fun sequence when I don't screw it up.
Bill K
Also I wonder sometimes if it would be "Legit" to say the first number, preceding 1100 is 111111111111... You never hear of it being put that way, but I think it works.
Bill K
A: 

I'd suggest the Writing Great Code Volumes I and II books. All software sits on top of a layer of abstraction. Learning how the layer below you works will give you an immediate and permanent improvement in productivity. For most software engineers this means learning how their computer handles data and executes the instructions the compiler emits. These two books by Randall Hyde are aimed exactly at teaching you these topics.

Andrew O'Reilly
A: 

Hacker's Delight

Topics covered include:

  • A broad collection of useful programming tricks
  • Small algorithms for common tasks
  • Power-of-2 boundaries and bounds checking
  • Rearranging bits and bytes
  • Integer division and division by constants
  • Some elementary functions on integers
  • Gray code
  • Hilbert's space-filling curve
  • And even formulas for prime numbers
Marco Mariani
+1  A: 

If you want to learn about all the weird tricks with bits, Bit Twiddling Hacks is an awesome resource. Some of the tricks there are truly mind-boggling.

SF.