integer-promotion Questions

4

Solved

I've read cppreference.com's implicit conversion: Integral promotion: prvalues of small integral types (such as char) may be converted to prvalues of larger integral types (such as int). [...] No...
Permission asked 24/8, 2023 at 12:46

1

Solved

I wonder why a - b and a + (-b) give the same result but in different types in numpy: import numpy as np minuend = np.array(1, dtype=np.int64) subtrahend = 1 << 63 result_minus = minuend - ...
Doubletongued asked 4/8, 2023 at 15:1

0

Consider an unsigned char v that goes through a series of bit-wise operations with the result stored back to v. Under the hood, it is integer promoted once, undergoes a series of operations, and th...
Congregation asked 10/5, 2023 at 14:9

2

Solved

For the code: int hi(int); int hi(); int main() { hi(3); } I don't get any compilation errors (calling hi(); without arguments does get a compilation error). I expected that the compiler would co...
Whichsoever asked 3/5, 2023 at 6:38

3

Solved

uint32_t a = 10; uint16_t b = 0; if (a > b) { printf("a > b\n"); } I was expecting that b would get promoted to int and the compiler would complain about comparing signed and uns...
Pumping asked 13/3, 2023 at 16:6

2

Solved

Consider the following listing: #include <type_traits> #include <cstdint> static_assert(std::is_same_v<decltype(31), int32_t>); static_assert(std::is_same_v<decltype(31u), uin...
Delectable asked 5/1, 2023 at 13:49

3

Solved

I encountered a strange problem, but to make it clear see the code first: #include <stdio.h> #include <stdint.h> int main() { uint8_t a = 0b1000'0000; // -> one leftmost bit uint8...

2

Solved

struct Type { uint8_t var : 3; }; int main() { struct Type bar; bar.var = 1; uint8_t baz = bar.var << 5; } According to the standard, left shifting more than the width of the left opera...

1

Solved

I'm currently writing some code for embedded systems (both in c and c++) and in trying to minimize memory use I've noticed that I used a lot of code that relies on integer promotions. For example (...
Locoism asked 3/3, 2022 at 16:36

6

Solved

I am reading some code that implements a simple parser. A function named scan breaks up a line into tokens. scan has a static variable bp that is assigned the line to be tokenized. Following the as...
Seidel asked 24/5, 2021 at 19:5

3

Solved

Do I understand the standard correctly that this program cause UB: #include <stdio.h> int main(void) { char a = 'A'; printf("%c\n", a); return 0; } When it is executed on a sys...
Dissimilation asked 15/9, 2020 at 19:20

2

When one performs a bitwise operation on an arithmetic type smaller than int, it is automatically promoted to int. std::uint8_t a = 42; auto b = a | 0x0f; // b will be of type int What I haven't b...

5

#include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<int> v = {1, 2, 3, 4, 5, 6, 7}; int i = -4; cout << i <...
Retriever asked 15/6, 2020 at 6:8

3

Solved

I have the following code: unsigned char x = 255; printf("%x\n", x); // ff unsigned char tmp = x << 7; unsigned char y = tmp >> 7; printf("%x\n", y); // 1 unsigned char z = (x <&l...

1

Solved

I was a bit messing around with uint8_t and was curious what happens when I outflow bits to the left and found that uint8_t i = 234; uint8_t j = (i << 1); auto k = (i << 1); s...
Serieswound asked 22/8, 2019 at 12:22

3

Solved

The UINT8_C macro is defined in "stdint.h", with the following specification: The macro UINTN_C(value) shall expand to an integer constant expression corresponding to the type uint_leastN_t. In th...
Zapateado asked 8/8, 2019 at 16:37

2

I'm trying to understand how integer promotion and comparison in a c++ application works. #include <cstdint> int main(void) { uint32_t foo = 20; uint8_t a = 2; uint8_t b = 1; uint8_t c =...
Ipa asked 27/6, 2019 at 18:30

2

Solved

I was writing some code recently that was actually supposed to test other code, and I stumbled upon a surprising case of integer promotion. Here's the minimal testcase: #include <cstdint> #i...
Barstow asked 10/5, 2019 at 0:43

3

Solved

I have two builds for a piece of software I'm developing, one for an embedded system where the size of an int is 16 bits, and another for testing on the desktop where the size of an int is 32 bits....
Ensnare asked 1/4, 2019 at 8:28

2

Solved

I need to eliminate gcc -Wconversion warnings. For example typedef unsigned short uint16_t; uint16_t a = 1; uint16_t b = 2; b += a; gives warning: conversion to 'uint16_t {aka short unsigned i...
Malar asked 7/2, 2019 at 13:41

3

Solved

I'm using IAR Workbench compiler with MISRA C:2004 checking on. The fragment is: #define UNS_32 unsigned int UNS_32 arg = 3U; UNS_32 converted_arg = (UNS_32) arg; /* Error line --> */ UNS_32 i...
Maximilianus asked 4/2, 2012 at 1:47

1

Solved

void foo(void) { unsigned int a = 6; int b = -20; if (a+b > a) { printf("> a"); } else { printf("< a"); } } I am trying to understand what is going on with the integer prom...
Maury asked 27/2, 2018 at 4:12

2

Solved

Imagine this situation. int32_t is an extended integer type and it's represented in two's complement (as the standard required int32_t to be represented). This means that INT32_MIN is -2147483648 (...
Dominga asked 28/12, 2017 at 17:18

3

Solved

7.16.1.1 2 describes va_arg as following (emphasis mine): If there is no actual next argument, or if type is not compatible with the type of the actual next argument (as promoted according to ...

1

Solved

I have the following C code which works: int ex(unsigned int x) { int mask = 0x55555555; int a = ((x >> 0) & mask ); return a + ((x >> 1) & mask ); } However, when I expan...
Dupion asked 22/9, 2017 at 15:48

© 2022 - 2025 — McMap. All rights reserved.