Bit (Definition)

A binary digit.

A Boolean value can be represented as a bit. Since a bit can maintain exactly two states (true or false), the two map very well to one another. Strictly speaking a Boolean value may yield an undefined state as well.

A continuous series of 8 bits forms a byte and 16 form a word. In JavaScript, 16 bit values tend to be the smallest that you operate with and correspond to a single character in a Unicode string. However, you can probably represent most characters that you want to use in the English language with only 8 bits. In fact only 7 bits are sufficient to describe your script source text in an ASCII representation.

Bit manipulation of character values allows you to convert between upper and lower case. The String.toUpperCase() and String.toLowerCase() methods allow you to convert specifically to the case you want, but if the current case is unknown and you simply want to toggle the case of a character, the difference between 'A' and 'a' is a single bit.

In most cases, you won't be operating with binary digits in JavaScript-based projects. However, the language is quite capable of working with bit patterns provided you understand how they work.

Although you cannot store an individual bit on its own, you can keep collections of 32 of them in a Number value.

In C language you operate on these using Bit-Fields. JavaScript does not support bit-fields but the sort of things you do with them can be simulated.

This is likely to be of most use to people developing scripts for use in embedded interpreters and of less use to browser script developers.

Example code:

   // Demonstrate bit inversion to change character case
   myString = "AbCdEfGh";
   myLength = myString.length;
   document.write("Original source string : ");
   document.write("<TABLE BORDER=1><TR><TH>");
   document.write("Orig char</TH><TH>");
   document.write("Char code</TH><TH>");
   document.write("Bit inverted<BR>char code</TH><TH>");
   document.write("New char</TH></TR>");
   for(myEnum = 0; myEnum < myLength; myEnum++)
      myChar     = myString.charAt(myEnum);
      myCharCode = myString.charCodeAt(myEnum);
      myNewCharCode = myCharCode ^ 32;

See also:Bit-field, String.toLocaleLowerCase(), String.toLocaleUpperCase(), String.toLowerCase(), String.toUpperCase()