Java的"位移动·QUOT;教程? [英] Java "Bit Shifting" Tutorial?

查看:90
本文介绍了Java的"位移动·QUOT;教程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会心存感激的一个很好的教程,在java中如何对Java新手解释所有的位转移工作。

我总是通过它绊倒,但永远无法理解它是如何工作。这里需要说明一下所有可能在Java byteshifting / bitmanipulation的操作和概念。

这是我的意思,(但我要寻找解释每一个可能的操作教程)只是一个例子:

 字节B =(字节)(L>>(8  -  I< 3;));


解决方案

那么,官方的Java教程的位运算和位移操作符涵盖了Java中可用的实际操作,以及如何调用它们。

如果你想知道我能做到与位移是什么,那么这是不是Java特有的,因为它是一个低级别的技术,我不知道很酷的事情,你可以在每做任何名单本身。这将会是值得熟悉的定义,并保持你的眼睛打开其他code,其中本时,看到他们做了什么。

请注意,往往位变换为清晰度为代价的效率增益。例如, A<< 1 通常是一样的 A * 2 但可以说不太清楚。重复异或可以交换两个号码,而无需使用一个临时变量,但它通常被认为是更好的形式来写code更清晰的的临时变量(甚至更好,在一个实用的方法)。所以在这方面很难给出很好的例子,因为你可能不会达到任何新的或深刻的架构水平;它是所有关于低级别的细节。 (而且我估计,野生位变换的用途的广大是premature优化实例。)

I would be thankfull for a good tutorial, that explain for java newbies how in java all the "bit shifting" work.

I always stumble across it, but never understood how it works. It should explain all the operations and concepts that are possible with byteshifting/bitmanipulation in java.

This is just an example what I mean, (but I am looking for a tutorial that explains every possible operation):

byte b = (byte)(l >> (8 - i << 3));

解决方案

Well, the official Java tutorial Bitwise and Bit Shift Operators covers the actual operations that are available in Java, and how to invoke them.

If you're wondering "what can I do with bit-shifting", then that's not Java specific, and since it's a low-level technique I'm not aware of any list of "cool things you can" do per se. It'd be worth becoming familiar with the definitions, and keeping your eyes open for other code where this is used, to see what they've done.

Note that often bit-twiddling is an efficiency gain at the expense of clarity. For example, a << 1 is usually the same as a * 2 but arguably less clear. Repeated XORs can swap two numbers without using a temporary variable, but it's generally considered better form to write the code more clearly with the temporary variable (or even better, in a utility method). So in this respect it's hard to give great examples, because you're not likely to achieve anything new or profound on an architecture level; it's all about the low-level details. (And I'd estimate that a vast number of uses of bit-twiddling "in the wild" are instances of premature optimisation.)

这篇关于Java的&QUOT;位移动·QUOT;教程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆