Hipster1206
Hipster1206

Reputation: 421

Buffer overflow with integers

I have a very basic question. Lets say I have two variables(uint16_t a, uint16_t b) and in memory they are aligned next to each other like a=> 0x0 => 0x15 and b=> 0x16 to 0x31

Lets assume a = 0, b = 65535,

(1) if i increment b(b++), b will become 0 but will it affect 'a' 0th bit?
(2) if i right shift b( b = b << 1), will it affect 'a' ?

Thank you

Upvotes: 1

Views: 156

Answers (3)

BooleanBuckeye
BooleanBuckeye

Reputation: 1

No, a correctly designed system will not have that happen. Also, I will point out that your numeral notation is incorrect by common convention. 0x is generally used to notate hexadecimal numbers, including in the C language, but from the context of your question, you are prefixing decimal base numbers with it for no apparent reason. For example, 0x31 is equal to 49 in decimal. And 16+16 is not equal to 49.

Upvotes: 0

The answer is no.

a and b is a uint16_t, so it is of an unsigned type. And unsigned overflow (or wrap-around) is well-defined in C. It won't change the memory besides it.

Upvotes: 0

AShelly
AShelly

Reputation: 35540

No, unless you are doing odd things with pointers or casts.

Upvotes: 2

Related Questions