2018年7月30日 星期一

Unsigned and signed shift:logical shift and arithmetic shift

In C, we usually use left shift and right shift operation << and >> to operate multiple / divide to variable with power of 2,ex: 4<<2  = 16

But when we use the signed variable and the shift operation we must consider the singed bit.See the below example code.

Since the C language support the arithmetic left shift and logic right shift,  we must consider the signed bit to  avoid some bug in code.



Ref:
     1.Logical Vs. Arithmetic Shift
     2.C语言中的逻辑右移和算术左移

沒有留言:

張貼留言

Linux driver: How to enable dynamic debug at booting time for built-in driver.

 Dynamic debug is useful for debug driver, and can be enable by: 1. Mount debug fs #>mount -t debugfs none /sys/kernel/debug 2. Enable dy...