tags:

views:

540

answers:

14

Although I know the basic concepts of binary representation, I have never really written any code that uses binary arithmetic and operations.

I want to know

  • What are the basic concepts any programmer should know about binary numbers and arithmetic ? , and

  • In what "practical" ways can binary operations be used in programming. I have seen some "cool" uses of shift operators and XOR etc. but are there some typical problems where using binary operations is an obvious choice.

Please give pointers to some good reference material.

+3  A: 

Unless you're working with lower level stuff, or are trying to be smart, you never really get to play with binary stuff.

I've been through a computer science degree, and I've never used any of the binary arithmetic stuff we learned since my course ended.

Have a squizz here: http://www.swarthmore.edu/NatSci/echeeve1/Ref/BinaryMath/BinaryMath.html

glasnt
+3  A: 

Hi

I believe basic know-hows on binary operations line AND, OR, XOR, NOT would be handy as most of the programming languages support these operations in the form of bit-wise operators.

These operations are also used in image processing and other areas in graphics.

One important use of XOR operation which I can think of is Parity check. Check this http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/BitOp/xor.html

cheers

Andriyev
Xor is also often used in random number generators :-)
Jasper Bekkers
To elaborate on Jasper's comment: Xor is used in linear feedback shift registers, which can be used as a pseudo-random number generator
David Johnstone
A: 

This really depends on the language you're using. Recent languages such as C# and Java abstract the binary representation from you -- this makes working with binary difficult and is not usually the best way to do things anyway in these languages.

Middle and low level languages like C and C++, however, require you to understand quite a bit about how the numbers are stored underneath -- especially regarding endianness.

Binary knowledge is also useful when implementing a cross platform protcol of some sort .... for example, on x86 machines, byte order is little endian. but most network protocols want big endian numbers. Therefore you have to realize you need to do the conversion for things to go smoothly. Many RFCs, such as this one -> http://tools.ietf.org/html/rfc4648 require binary knowledge to understand.

In short, it's completely dependent on what you're trying to do.

Billy3

Billy ONeal
A: 

It's handy to know the numbers 256 and 65536. It's handy to know how two's complement negative numbers work.

Maybe you won't run into a lot of binary. I still use it pretty often, but maybe out of habit.

A good familiarity with bitwise operations should make you more facile with boolean algebra, and I think that's important for every programmer--you want to be able to quickly simplify complex logic expressions.

Nosredna
+2  A: 

You don't specifically mention (nor rule out!-) floating point binary numbers and arithmetic, so I won't miss the opportunity to flog one of my favorite articles ever (seriously: I sometimes wish I could make passing a strict quiz on it a pre-req of working as a programmer...;-).

Alex Martelli
+3  A: 

If you are developing lower-level code, it is critical that you understand the binary representation of various types. You will find this particularly useful if you are developing embedded applications or if you are dealing with low-level transmission or storage of data.

That being said, I also believe that understanding how things work at a low level is useful even if you are working at much higher levels of abstraction. I have found, for example, that my ability to develop efficient code is improved by understanding how things are represented and manipulated at a low level. I have also found such understanding useful in working with debuggers.

Here is a short-list of binary representation topics for study:

  • numbering systems (binary, hex, octal, decimal, ...)
  • binary data organization (bits, nibbles, bytes, words, ...)
  • binary arithmetic
  • other binary operations (AND,OR,XOR,NOT,SHL,SHR,ROL,ROR,...)
  • type representation (boolean,integer,float,struct,...)
  • bit fields and packed data

Finally...here is a nice set of Bit Twiddling Hacks you might find useful.

Brandon E Taylor
+1  A: 

The following are things I regularly appreciate knowing in my quite conventional programming work:

  • Know the powers of 2 up to 2^16, and know that 2^32 is about 4.3 billion. Know them well enough so that if you see the number 2147204921 pop up somewhere your first thought is "hmm, that looks pretty close to 2^31" -- that's a very effective module for your bug radar.

  • Be able to do simple arithmetic; e.g. convert a hexadecimal digit to a nybble and back.

  • Have some vague idea of how floating-point numbers are represented in binary.

  • Understand standard conventions that you might encounter in other people's code related to bit twiddling (flags get ORed together to make composite values and AND checks if one's set, shift operators pack and unpack numbers into different bytes, XOR something twice and you get the same something back, that kind of thing.)

Further knowledge is mostly gravy unless you work with significant performance constraints or do other less common work.

mquander
+2  A: 

At the absolute bare minimum you should be able to implement a bit mask solution. The tasks associated with bit mask operations should ensure that you at least understand binary at a superficial level.

Matthew Vines
+2  A: 

The most important thing every programmer should know about binary numbers and arithmetic is : Every number in a computer is represented in some kind of binary encoding, and all arithmetic on a computer is binary arithmetic.

The consequences of this are many:

  • Floating point "bugs" when doing math with IEEE floating point binary numbers (Which is all numbers in javascript, and quite a few in JAVA, and C)
  • The upper and lower bounds of representable numbers for each type
  • The performance cost of multiplication/division/square root etc operations (for embedded systems
  • Precision loss, and accumulation errors

and more. This is stuff you need to know even if you never do a bitwise xor, or not, or whatever in your life. You'll still run into these things.

Breton
A: 

Absolute minimum is, that "2" is not a binary digit and 10b is smaller than 3.

zeroDivisible
+3  A: 

From the top of my head, here are some examples of where I've used bitwise operators to do useful stuff.

A piece of javascript that needed one of those "check all" boxes was something along these lines:

var check = true;
for(var i = 0; i < elements.length; i++)
   check &= elements[i].checked;
checkAll.checked = check;

Calculate the corner points of a cube.

Vec3f m_Corners[8]; 

void corners(float a_Size){ 
    for(size_t i = 0; i < 8; i++){ 
        m_Corners[i]  = a_Size * Vec3f(axis(i, Vec3f::X), axis(i, Vec3f::Y), axis(i, Vec3f::Z)); 
    } 
} 

float axis(size_t a_Corner, int a_Axis) const{ 
    return ((a_Corner >> a_Axis) & 1) == 1 
        ? -.5f 
        : +.5f; 
}

Draw a Sierpinski triangle

for(int y = 0; y < 512; y++)
  for(int x = 0; x < 512; x++)
     if(x & y) pixels[x + y * w] = someColor;
     else pixels[x + y * w] = someOtherColor;

Finding the next power of two

int next = 1 << ((int)(log(number) / log(2));

Checking if a number is a power of two

bool powerOfTwo = number & (number - 1);

The list can go on and on, but for me these are (except for Sierpinksi) everyday examples. Once you'll understand and work with it though, you'll encounter it in more and more places such as the corners of a cube.

Jasper Bekkers
A: 

If you never do low-level programming (like C in embedded systems), never have to use a debugger, and never have to work with real numbers, then I suppose you could get by without knowing binary. But knowing binary will make you a stronger programmer, even if indirectly.

Once you venture into those areas you will need to know binary (and its ``sister'' base, hexadecimal). Without knowing it:

  • Embedded systems programming would be impossible.

  • Debugging would be hard because you wouldn't know what you were looking at in memory.

  • Numerical calculations with decimals would give you answers you don't understand.

Rick Regan
+2  A: 

You must understand bit masks.

Many languages and situations require the use of bit masks, for example flags in arguments or configs.

PHP has its error level which you control with bit masks:

error_reporting = E_ALL & ~E_NOTICE

Or simply checking if an int is odd or even:

isOdd = myInt & 1
truppo
A: 

I learned to twiddle bits back when c and asm were still used for "mainstream" programming. Although I no longer have much use for that knowledge, I recently used it to solve a real-world business problem.

We use a fax service that posts a message back to us when the fax has been sent or failed after x number of retries. The only way I had to identify the fax was a 15 character field. We wanted to consolidate this into one URL for all of our clients. Before we consolidated, all we had to fit in this field was the FaxID PK (32 bit int) column which we just sent as a string.

Now we had to identify the client (a 4 character code) and the database (32 bit int) underneath the client. I was able to do this using base 64 encoding. Without understanding the binary representation of numbers and characters, I probably would never have even thought of this solution.

JohnOpincar