开发者

what is the meaning of "<<" in TCL?

I know the "<<" is a开发者_StackOverflow社区 bit operation. but I do not understand what it exactly functions in TCL, and when should we use it?

can anyone help me on this?


The << operator in Tcl's expressions is an arithmetic bit shift left. It's exceptionally similar to the equivalent in C and many other languages, and would be used in all the same places (it's logically equivalent to a multiply by a suitable power of 2, but it's usually advisable to use a shift when thinking about bits and a multiply when thinking about numbers).

Note that one key difference with many other languages (from Tcl 8.5 onwards) is that it does not “drop bits off the front”; the language implementation automatically uses wider number representations as necessary so that information is never lost. Bits are dropped by using a separate binary mask operation (e.g., & ((1 << $numBits) - 1)).


There are a number of uses for the << shift left operator. Some that come to my mind are :

  • Bit by bit processing. Shift a number and observe highest order bit etc. It comes in more handy than you might think.
  • If you add a zero to a number in the decimal number system you effectively multiply it by 10. shifting bits effectively means multiplying by 2. This actually translated into a low level assembly command of bit shifting which has lower compute cycles than multiplication by 2. This is used for efficiency in the gaming industry. Shift if twice (<< 2) to multiply it by 4 and so on.

I am sure there are many others.


The << operation is not much different from C's, for instance. And it's used when you need to shift bits of an integer value to the left. This can be occasionally useful when doing subtle number crunching like implemening a hash function or deserialising something from an input bytestream (but note that [binary scan] covers almost all of what's needed for this sort of thing). For a more general info refer to this Wikipedia article or something like this, this is not really Tcl-related.


The '<<' is a left bit shift. You must apply it to an integer. This arithmetic operator will shift the bits to left. For example, if you want to shifted the number 1 twice to the left in the Tcl interpreter tclsh, type:

 expr { 1 << 2 } 

The command will return 4.

Pay special attention to the maximum integer the interpreter hold on your platform.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜