C language cuts constants from 0-255 or any 2^n - 1 constants (a curious trick)

C language cuts constants from 0-255 or any 2^n - 1 constants (a curious trick)

Crop any constant of 2^n - 1, less than 0 then 0, greater than 2^n - 1 then 2^n - 1, e.g. crop the range 0 - 65535.static inline int32_t cut_65535(int32_t x) {
	return (((0xFFFF - x) >> 31) | x) & 0xFFFF;
}

Comments

Popular posts from this blog

Python Receiving and parse JSON Data via UDP protocol

ubus lua client method and event registration code demo/example