为什么这个uint32_t -2147483648的值?

时间:2017-08-21 17:31:47

标签: c binary uint32

使用gcc编译器,为什么在设置MSB时这是uint32_t -2147483648?它没有签名 - 不应该是积极的吗?

#include <stdio.h>
#include <stdint.h>

#define BIT_SET(a,b) ((a) |= (1<<(b)))

int main()
{
    uint32_t var = 0;
    BIT_SET(var, 31);

    printf("%d\n", var); //prints -2147483648

    return 0;
}

4 个答案:

答案 0 :(得分:7)

ScrollView适用于已签名整数。你想要的是document.referrer,它是无符号整数。 %d smart 不足以猜测您的数据类型。

Source

答案 1 :(得分:1)

printf()不知道您传递的内容的类型,并且信任您使用正确的转换说明符。但你不是。使用%dprintf()会将您的值解释为int

unsigned intuint32_t(非常常见)相同的平台上,您可以改用%u。但请注意,这不是便携式的,例如到int只有16位的平台。打印uint32_t的正确方法如下(您必须包含inttypes.h):

printf("%" PRIu32 "\n", var);

参见例如this inttypes.h reference

答案 2 :(得分:0)

您在此处使用错误的格式说明符。 function getPosts() { $posts = array(); $posts[0] = $_POST['$id']; $posts[1] = $_POST['$studentID']; $posts[2] = $_POST['$firstN']; $posts[3] = $_POST['$middleN']; $posts[4] = $_POST['$lastN']; $posts[5] = $_POST['$course']; $posts[6] = $_POST['$year']; $posts[7] = $_POST['$PoBirth']; $posts[8] = $_POST['$address']; $posts[9] = $_POST['$birthday']; $posts[10] = $_POST['$acr']; $posts[11] = $_POST['$gender']; $posts[12] = $_POST['$nationality']; $posts[13] = $_POST['$mobile']; $posts[14] = $_POST['$status']; $posts[15] = $_POST['$schoolyear']; $posts[16] = $_POST['$guardianN']; $posts[17] = $_POST['$guardianM']; $posts[18] = $_POST['$relationship']; $posts[19] = $_POST['$occupation']; $posts[20] = $_POST['$guardianAddress']; return $posts; } 用于%d类型,即有符号整数类型。

要打印精确的宽度变量,您需要使用int宏作为格式说明符。

对于无符号类型32位,那将是PRIuN。有关详尽列表,请参阅章节§7.8.1,PRIu32

答案 3 :(得分:0)

JSONObject jObject = null; try { jObject = new JSONObject(response); JSONArray jArray = jObject.getJSONArray("questions"); JSONObject jsonObject=jArray.getJSONObject(0); String question = jsonObject.getString("question"); } catch (JSONException e) { e.printStackTrace(); } 的类型为var。但是您使用{{1>}打印它,这是未定义的行为。使用uint32_t中的%d打印PRIu32

<inttypes.h>