“<<”是什么意思?在TCL?

发布于 2024-11-26 03:23:29 字数 90 浏览 1 评论 0 原文

我知道“<<”是一个位运算。但我不明白它在TCL中到底有什么作用,什么时候应该使用它?

谁能帮我解决这个问题吗?

I know the "<<" is a 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情魔剑神 2024-12-03 03:23:29

Tcl 表达式中的 << 运算符是一个算术左移。它与 C 和许多其他语言中的等效项非常相似,并且可以在所有相同的地方使用(它在逻辑上相当于乘以适当的 2 次幂,但通常建议在考虑位和 a 时使用移位)当考虑数字时乘以)。

请注意,与许多其他语言(从 Tcl 8.5 开始)的一个关键区别是它不会“从前面删除位”;语言实现会根据需要自动使用更广泛的数字表示,以便信息永远不会丢失。通过使用单独的二进制掩码操作(例如,& ((1 << $numBits) - 1))来丢弃位。

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)).

好倦 2024-12-03 03:23:29

<< 有多种用途。左移运算符。我想到的一些是:

  • 一点一点的处理。移动数字并观察最高位等。它比您想象的更方便。
  • 如果在十进制数字系统中的数字上添加一个零,则实际上将其乘以 10。移位实际上意味着乘以 2。这实际上转换为移位的低级汇编命令,其计算周期比乘以 2 更低。这用于提高游戏行业的效率。移位 if 两次 (<< 2) 将其乘以 4,依此类推。

我确信还有很多其他人。

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.

蝶舞 2024-12-03 03:23:29

例如,<< 操作与 C 的操作没有太大区别。当您需要将整数值向左移动时使用它。当进行微妙的数字运算时,例如实现哈希函数或从输入字节流中反序列化某些内容,这有时会很有用(但请注意,[binary scan]涵盖了此类事情所需的几乎所有内容)。有关更一般的信息,请参阅这篇维基百科文章或类似的内容,这并不是真正的Tcl相关。

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.

握住我的手 2024-12-03 03:23:29

'<<'是左移位。您必须将其应用于整数。该算术运算符会将位左移。
例如,如果要在 Tcl 解释器 tclsh 中将数字 1 向左移动两次,请键入:

 expr { 1 << 2 } 

该命令将返回 4。

请特别注意解释器在您的平台上保存的最大整数。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文