C job interview - casting and comparing

后端 未结 10 726
没有蜡笔的小新
没有蜡笔的小新 2021-02-01 12:29

I was confronted with a tricky (IMO) question. I needed to compare two MAC addresses, in the most efficient manner.

The only thought that crossed my mind in that moment

10条回答
  •  星月不相逢
    2021-02-01 13:06

    This would work on most systems,and be faster than your solution.

    int isEqual(MAC* addr1, MAC* addr2)
    {
        return ((int32*)addr1)[0] == ((int32*)addr2)[0] &&  ((int16*)addr1)[2] == ((int16*)addr2)[2];
    }
    

    would inline nicely too, could be handy at the center of loop on a system where you can check the details are viable.

提交回复
热议问题