Have you ever had to use bit shifting in real projects?

后端 未结 30 3749
故里飘歌
故里飘歌 2020-12-02 05:35

Have you ever had to use bit shifting in real programming projects? Most (if not all) high level languages have shift operators in them, but when would you actually need to

相关标签:
30条回答
  • 2020-12-02 06:06

    When converting numbers from little endian to the big endian format and vice versa

    0 讨论(0)
  • 2020-12-02 06:06

    Bit shifting is used a lot in deciphering the protocols of online games. The protocols are designed to use a little bandwidth as possible, so instead of transmitting the number of players on a server, names and so forth in int32s, all the information is packed into as few bytes as possible. It's not really necessary these days with most people using broadband, but when they were originally designed people used 56k modems for gaming, so every bit counted.

    The most prominent examples of this are in Valve's multiplayer games particularly Counter-Strike, Counter-Strike Source. The Quake3 protocol is also the same, however Unreal isn't quite as slimline.

    Here's an example (.NET 1.1)

    string data = Encoding.Default.GetString(receive);
    
    if ( data != "" )
    {
        // If first byte is 254 then we have multiple packets
        if ( (byte) data[0] == 254 )
        {
            // High order contains count, low order index
            packetCount = ((byte) data[8]) & 15; // indexed from 0
            packetIndex = ((byte) data[8]) >> 4;
            packetCount -= 1;
    
            packets[packetIndex] = data.Remove(0,9);
        }
        else
        {
            packets[0] = data;
    
        }
    }
    

    Of course whether you view this as a real project or just a hobby (in C#) is up to you.

    0 讨论(0)
  • 2020-12-02 06:06

    Bit shifting is also required when communicating with "lower level" equiment, eq digital ethernet-IO -boxes or PLC's, which usually pack invidual input/output values into bytes.

    0 讨论(0)
  • 2020-12-02 06:07
    • Creating nice flag values for the enums (rather than typing manually 1, 2, 4...)
    • Unpacking the data from the bit-fields (many network protocols use them)
    • Z-curve traversal
    • Performance hacks

    And I cannot think of many cases when they are being used. It's usually other way around - there is some specific problem, and it turns out that employing bit operations will yield the best results (usually in term of performance - time and/or space).

    0 讨论(0)
  • 2020-12-02 06:07

    Yes, all the time. Like these macros for packing and unpacking a 3space coordinate to/from a 32-bit integer:

    #define Top_Code(a, b, c)           ((((a) + x) << 20) | (((b) + y) << 10) | ((c) + z))                           
    #define From_Top_Code(a, b, c, f)   (a = (((f) >>> 20) - x), b = ((((f) & 0xffc00) >>> 10) - y), c = (((f) & 0x3ff) - z))        
    
    0 讨论(0)
  • 2020-12-02 06:08

    I have seen bitwise operators used when multiple flags were used as a property parameter. For example number 4 = 1 0 0 means that one of the three flags is set. This is not good for public API but it can speed up things in special cases since checking for bits is fast.

    0 讨论(0)
提交回复
热议问题