HOME - - - - - - - - Table of contents, my Arduino "How To" articles
Other material for programmers Delicious.Com Bookmark this on Delicious    StumbleUpon.Com Recommend to StumbleUpon

Binary numbers: Setting and clearing individual bits

This is a rough copy of something that I will, maybe, if I get the chance... (excuses, excuses)... make more "reader friendly" one day. In the meantime, there is useful information here!

In some lines of microcontroller work, you need to work with the individual binary digits, ("bits"... get it?.... binay digits)) of some of the numbers stored in your machine.

Don't get hung up on the "is it in binary? decimal? ASCII?" thing... every number is all three. "Dozen" and "twelve" are still 12.

In my tutorials, I take a very crude, but, I hope, transparent approach.

Bill O'Neill, of Aztec MCU Prototyping kindly wrote in, giving some more elegant answers. I reproduce his email below. Aztec have a number of useful modules, and, for a fee, can do design consulting.

Hi Tom,

In your code (in several places I think) you do something like this:

  //The code is CRUDE... but it works!
  //I won't be offended if someone writes me
  //   to tell me the right way to do this,
  //   as long as the right way is transparent
  //   for newbies.
  temp_string[0]=32;
  temp_string[1]=32;
  temp_string[2]=32;
  temp_string[3]=32;
  temp_string[4]=48;
  temp_string[5]=0;

  if (iClicks>9999)
     {iTmp=(iClicks / 10000) % 10;
     temp_string[0]=48+iTmp;};
  if (iClicks>999)
     {iTmp=(iClicks / 1000) % 10;
     temp_string[1]=48+iTmp;};
  if (iClicks>99)
     {iTmp=(iClicks / 100) % 10;
     temp_string[2]=48+iTmp;};
  if (iClicks>9)
     {iTmp=(iClicks / 10) % 10;
     temp_string[3]=48+iTmp;};
  if (iClicks>0)
     {iTmp=iClicks % 10;
     temp_string[4]=48+iTmp;};
  //End of "put a number in the output buffer"

  i=0;
   while (temp_string[i]) {
           buf[TCP_CHECKSUM_L_P+3+plen]=temp_string[i++];
           plen++;
   }

===
All of that can be replaced with the following two lines....

  itoa(iClicks, temp_string, 10);
      //This is C's standard integer to ASCII conversion function

  plen=es.ES_fill_tcp_data(buf,plen,temp_string);
     //No need to iterate through the temp_string.
     //   itoa will append a null which will be interpreted
     //   correctly by the ES_fill_tcp_data function.




   Search this site or the web        powered by FreeFind
 
  Site search Web search
Site Map    What's New    Search

The search engine is not intelligent. It merely seeks the words you specify. It will not do anything sensible with "What does the 'could not compile' error mean?" It will just return references to pages with "what", "does", "could", "not".... etc.
In addition to the tutorials for which this page serves as Table of Contents, I have other sites with material you might find useful.....

Sequenced set of tutorials on Arduino programming and electronics interfacing.
Tutorials about the free database supplied with Open Office version 2. (If you experienced Adabas with Star Office, ooBase is nothing like it!)
Some pages for programmers.
Using the parallel port of a Windows computer.


If you visit 1&1's site from here, it helps me. They host my website, and I wouldn't put this link up for them if I wasn't happy with their service.




Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!

Click here to visit editor's Sheepdog Software (tm) freeware, shareware pages.


And if you liked that, or want different things, here are some more pages from the editor of these tutorials....

Click here to visit the homepage of my biggest site.

Click here to visit the homepage of Sheepdogsoftware.co.uk. Apologies if the "?FrmAht" I added to that link causes your browser problems. Please let me know, if so?

Click here to visit editor's pages about using computers in Sensing and Control, e.g. weather logging.



To email this page's editor, Tom Boyd.... Editor's email address. Suggestions welcomed!


Valid HTML 4.01 Transitional Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. Mostly passes. There were two "unknown attributes" in Google+ button code. Sigh.


Why does this page cause a script to run? Because of the Google panels, and the code for the search button. Why do I mention the script? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....