Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Explain this code? 2

Status
Not open for further replies.

Supra

Programmer
Dec 6, 2000
422
US
Hey folks,

I modified someone else's code which converted a decimal to hex so that it would convert a string to one which a web browser can understand - IE: A space is %20. Anyway, a couple bits of just 2 lines of the code I borrowed I do not understand. They are highlighted in red below. Can anyone explain?
Code:
var Base = '0123456789ABCDEF';

function alphaToHex(w) {
var h;
var i;
var j = "";
	for (c=0; c<w.length; c++) {
		i = w.charCodeAt(c);
		h = Base.substr([red]i&15[/red],1); [green]// What does [i]i[/i]&15 do?[/green]
			while(i > 15) {
				i [red]>>=[/red] 4; [green]// [i]i[/i] is greater than, is greater than and equal to 4..?  What?[/green]
				h = Base.substr(i&15,1) + h;
			}
		j += '%' + h;
	}
return j;
}
This is probably cake for the JavaScript gurus, but I have never seen these 2 practices before!
 
The >> operator means to shift right the specified number of bits, or divide by 2 to that power (in this case, 16). In this case, it means divide thus
Code:
i = i / 16;
while i > 15.

This
Code:
h = Base.substr(i & 15, 1) + h;
is using a bitwise AND and is equivalent to
Code:
h = Base.substr(i % 16, 1) + h;
to just strip off the last 4 bits of the number to work with.

If you're not familiar with bit operations, this could still be confusing. In some compilers and interpreters bitwise manipulation of numbers compiles to faster code than using regular math operators, but unless your code is performing thousands of math operations in that loop, I doubt that you'd see a difference at all in speed.

Lee
 
Lee,

Many years ago I programmed on one of the old UNIVAC computers and I remember shift left and shift right were a couple of opcodes we had available. I've seen other languages that had similar functions. Never could fathom a reason why one would want to shift like that until I read "...or divide by 2 to that power (in this case, 16)." Why yes, of course, it makes logical sense now.

Have a star.

Larry
 
Thanks Lee!

I have to admit, it's still a little confusing, but at least I think I know what they do. Doesn't the % operator just get the remainder of the division? So if i = 10 % 8, i = 2, correct? I think I need to get more involved with these bitwise operations, as they are very intriguing. Thanks for the excellent definition of this code!!

Definitely worth a star :)
 
Yes, the % is remainder/modulo division. I generally use that rather than bit operations in this kind of situation because it's easier for others to maintain who don't have experience with the trickier stuff.

Lee
 
Just butting into this conversation ;-)

Using the & operator is great for bitmaps, etc.

var optCanDo1 = 1
var optCanDo2 = 2
var optCanDo3 = 4
var optCanDo4 = 8
var optCanDo5 = 16 // powers of 2

var myOpts = 25 // 16 + 8 + 1

if (myOpts & optCanDo1) do1();
if (myOpts & optCanDo2) do2();
if (myOpts & optCanDo3) do3();
if (myOpts & optCanDo4) do4();
if (myOpts & optCanDo5) do5();

do1(), do4() and do5() are run.

This is where using % would not work.

Chaz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top