ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java优先级 – 转换和按位运算符

2019-08-27 23:03:53  阅读:322  来源: 互联网

标签:java operators operator-precedence bit-manipulation


我很难理解一些代码,这些代码显示了如何将Java中的double转换为byte []的示例,反之亦然.

以下是用于将double转换为byte []的代码:

public static byte [] doubleToByteArray (double numDouble)
{
    byte [] arrayByte = new byte [8];
    long numLong;

    // Takes the double and sticks it into a long, without changing it
    numLong = Double.doubleToRawLongBits(numDouble);

    // Then we need to isolate each byte
    // The casting of byte (byte), captures only the 8 rightmost bytes
    arrayByte[0] = (byte)(numLong >>> 56);
    arrayByte[1] = (byte)(numLong >>> 48);
    arrayByte[2] = (byte)(numLong >>> 40);
    arrayByte[3] = (byte)(numLong >>> 32);
    arrayByte[4] = (byte)(numLong >>> 24);
    arrayByte[5] = (byte)(numLong >>> 16);
    arrayByte[6] = (byte)(numLong >>> 8);
    arrayByte[7] = (byte)numLong;

    for (int i = 0; i < arrayByte.length; i++) {
        System.out.println("arrayByte[" + i + "] = " + arrayByte[i]);
    }

    return arrayByte;
}

这里是用于将byte []转换回double的代码:

public static double byteArrayToDouble (byte [] arrayByte)
{
    double numDouble;
    long numLong;

    // When putting byte into long, java also adds the sign 
    // However, we don't want to put bits that are not from the orignal value
    // 
    // The rightmost bits left unaltered because we "and" them with a 1
    // The left bits become 0 because we "and" them with a 0
    //
    // We are applying a "mask" (& 0x00 ... FFL)
    // 0 & 0 = 0
    // 0 & 1 = 0
    // 1 & 0 = 0
    // 1 & 1 = 1
    //
    // So, the expression will put byte in the long (puts it into the right most position)
    // Then we apply mask to remove the sign applied by java
    // Then we move the byte into its position (shift left 56 bits, then 48 bits, etc.)
    // We end up with 8 longs, that each have a byte set up in the appropriate position
    // By doing an | with each one of them, we combine them all into the orignal long
    //
    // Then we use Double.longBitsToDouble, to convert the long bytes into double.
    numLong = (((long)arrayByte[0] & 0x00000000000000FFL) << 56) | (((long)arrayByte[1] & 0x00000000000000FFL) << 48) |
        (((long)arrayByte[2] & 0x00000000000000FFL) << 40) |    (((long)arrayByte[3] & 0x00000000000000FFL) << 32) |
        (((long)arrayByte[4] & 0x00000000000000FFL) << 24) | (((long)arrayByte[5] & 0x00000000000000FFL) << 16) |
        (((long)arrayByte[6] & 0x00000000000000FFL) <<  8) | ((long)arrayByte[7] & 0x00000000000000FFL);

    numDouble = Double.longBitsToDouble(numLong);

    return numDouble;
}

好的,这是我不太了解的部分.

((long)arrayByte[0] & 0x00000000000000FFL) << 56

似乎演员在实际的按位操作之前发生,因为作者说

the expression will put byte in the long […] Then we apply mask to remove the sign applied by java

为什么字节在实际转换之前被转换为很长时间?操作不应该像这样吗?

(((long)arrayByte[0]) & 0x00000000000000FFL) << 56

还是有其他我不明白的东西?

解决方法:

这是由于运算符优先级和关联性在Java.1中的工作原理

不幸的是,Oracle Java Tutorial只提供了部分概述,而Java Language Specification也没有太大的帮助,因为它主要是通过陈述以下方式来确定运算符优先级给读者:

Precedence among operators is managed by a hierarchy of grammar productions.

通常,表达式从左到右进行评估.在运算符优先级方面,以下表2适用:

╔═══════╦══════════════╦══════════════════════╦═════════════════╗
 Level  Operator      Description           Associativity   
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
 16     []            access array element  left to right   
        .             access object member                  
        ()            parentheses                           
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
 15     ++            unary post-increment  not associative 
        --            unary post-decrement                  
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
 14     ++            unary pre-increment   right to left   
        --            unary pre-decrement                   
        +             unary plus                            
        -             unary minus                           
        !             unary logical NOT                     
        ~             unary bitwise NOT                     
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
 13     ()            cast                  right to left   
        new           object creation                       
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
 12     *             multiplicative        left to right   
        /                                                   
        %                                                   
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
 11     + -           additive              left to right   
        +             string concatenation                  
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
 10     << >>         shift                 left to right   
        >>>                                                 
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
 9      < <=          relational            not associative 
        > >=                                                
        instanceof                                          
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
 8      ==            equality              left to right   
        !=                                                  
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
 7      &             bitwise AND           left to right   
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
 6      ^             bitwise XOR           left to right   
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
 5      |             bitwise OR            left to right   
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
 4      &&            logical AND           left to right   
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
 3      ||            logical OR            left to right   
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
 2      ?:            ternary               right to left   
╠═══════╬══════════════╬══════════════════════╬═════════════════╣
 1      = += -=       assignment            right to left   
        *= /= %=                                            
        &= ^= |=                                            
        <<= >>= >>>=                                        
╚═══════╩══════════════╩══════════════════════╩═════════════════╝

对于您的特定问题,这意味着不需要在强制转换操作周围放置额外的括号,因为强制转换运算符()的优先级高于按位AND和&的强度.运算符(13级与7级).

1我写这篇文章是一个规范的答案,用于解决有关Java中运算符优先级和关联性的问题.我找到了许多现有的答案,给出了部分信息,但我找不到一个概述完整优先级和关联表的人.
2运算符优先级和关联表从https://introcs.cs.princeton.edu/java/11precedence/再现.

标签:java,operators,operator-precedence,bit-manipulation
来源: https://codeday.me/bug/20190827/1745597.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有