想象一下,您將擁有數字的二進制或十六進制表示形式。讓我們藉此:Java中的按位運算子字符串
int number = 0xffffff;
// this would recover the third f, as a stand-alone value, value of third_f would be just f
int third_f = byteSubstring(number,2,1);
// third and fourth f, value of tf would be ff
int tf = byteSubstring(number,2,2);
// all except first, value of except_first would be fffff
int except_first = byteSubstring(number,1,5);
使用單獨的位操作,筆&紙,我知道如何提取所有的人,但他們在一個通用功能相結合... :)。 JDK中是否有可用於數字類型的東西?
其實,我不認爲你明白你非常寫什麼。 0xf是四位,二進制1111,而「第三個f」的值將是0x00f000' 0r'0x000f00',具體取決於你如何計算(你的except_first函數似乎是從1開始索引而不是0)。 – 2011-05-29 19:36:46