在C语言中读写整数最高位的最简便方法是什么? [英] What is the most portable way to read and write the highest bit of an integer in C?

查看:35
本文介绍了在C语言中读写整数最高位的最简便方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C语言中读写整数最高位的最简便方法是什么?

What is the most portable way to read and write the highest bit of an integer in C?

这是彭博社的采访问题.当时我没有给出最佳答案.有人可以回答吗?

This is a Bloomberg interview question. I didn’t give best answer at that time. Can anyone please answer it?

推荐答案

如果类型是无符号的,那么很简单:

If the type is unsigned, it's easy:

(type)-1-(type)-1/2

对于带符号的值,我不知道.如果您找到了一种方法,它将在SO上回答几个未解决的问题:

For signed values, I know no way. If you find a way, it would answer several unanswered questions on SO:

C问题:off_t(和其他有符号整数类型)的最小值和最大值

在编译时有没有办法计算整数类型的宽度?

也许其他人.

这篇关于在C语言中读写整数最高位的最简便方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆