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

后端 未结 30 3852
故里飘歌
故里飘歌 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:20

    Yes, still it's needed.

    Here in my job for example we develop softwares for comunication with PLC through the serial port COMx. It's necessary to handle bits within a byte, we use shift left / right, and logic operators OR,XOR,AND in day by day.

    For example, let's suppose that we need turn on the bit 3 (right to left) of a byte:

    It's much more efficient to do:

    Byte B;
    
    B := B XOR 4;
    

    Instead of:

    Byte B = 0;
    String s;  // 0 based index
    
    s = ConvertToBinary (B);
    s[5] = "1";
    B := ConvertToDecimal (s);
    

    Regards.

提交回复
热议问题