Skip to content

Syllabus · CS Computer Science and Information Technology

All topics in this subject
C programming foundations7
Arrays and pointers9
Recursion6
Probability and statistics foundations9
Database systems foundations15
Linear algebra foundations6
Computer networks foundations9
Calculus foundations6
Theory of computation foundations9
Compiler design foundations9
Linked lists, stacks and queues5
Trees, BSTs and heaps4
Graph foundations3
Algorithms foundations15
Discrete mathematics foundations18
Digital logic foundations22
Computer organization foundations12
Operating systems foundations15

C conversions, bitwise operations and sequencing

Lesson 7 of 1797 minFree

Objectives and language contract

Extend basic C expression tracing by deciding the types first, distinguishing unsigned wrap from signed overflow, and separating expression grouping from evaluation order. We use ISO C11/C17. No assumption of a 32-bit int, eight-bit byte, two's-complement signed representation or signed plain char is implicit. Individual numerical examples declare extra assumptions. Code fragments assume required standard headers, valid initialized objects and no concurrent accesses. A compiler's observed output cannot turn undefined behavior into a portable answer.

1. Promotions happen before arithmetic

The integer promotions convert char, signed char, unsigned char, short and unsigned short to int if int represents their entire range, otherwise to unsigned int. Thus two unsigned char operands need not add in unsigned-char arithmetic. Under CHAR_BIT=8 and int range at least −32767..32767, unsigned char a=250, b=10; a+b has type int and value 260. Assigning that result to unsigned char c converts modulo 256, giving 4. The expression and the destination object have different types.

For two integer operands of operators that use the usual arithmetic conversions (such as arithmetic + and relational <), first apply integer promotions and then those conversions. Shifts and logical operators have their own rules. Same signedness chooses the higher rank. For mixed signed/unsigned types: if unsigned rank is at least signed rank, use the unsigned type; otherwise use the signed type if it represents every value of the unsigned type, and the corresponding unsigned signed-type partner if it cannot. Rank and representable range matter; do not use the slogan “unsigned always wins” for every pair.

Worked comparison: −1 < 1u is false. Unary minus produces int −1; comparison converts it to unsigned int, obtaining UINT_MAX, which exceeds 1. This conclusion does not require 32 bits. By contrast, with a 64-bit long that represents every 32-bit unsigned int value, −1L < 1u is true because comparison uses long.

Integer division truncates toward zero: −7/3 is −2 and −7%3 is −1. The identity a=(a/b)b+a%b holds when the quotient is representable and b is nonzero. INT_MIN/−1 is undefined when the quotient is unrepresentable; division by zero is undefined. Conversion of an out-of-range integer to unsigned is modulo one more than the unsigned maximum. Conversion to a signed integer that cannot represent it has an implementation-defined result or raises an implementation-defined signal in C11/C17; it is not a portable wraparound rule. A floating-to-integer conversion discards the fractional part, but an unrepresentable integral result is undefined: integer modulo conversion must not be generalized to floating operands.

2. Bitwise operations require a width/type contract

Use unsigned operands for predictable bit patterns. For unsigned int x=12u (binary low bits 1100), x & 10u=8u, x | 3u=15u and x ^ 10u=6u. Logical x && 10u instead yields int 1 because both operands are nonzero. Bitwise complement ~x flips every bit of the promoted type, not merely the four written digits. In particular ~0u equals UINT_MAX.

When the promoted left operand has unsigned type with W value bits, left shift by k is multiplication by 2^k modulo 2^W, and right shift is division by 2^k rounded down, provided 0≤k<W. Here W counts the value bits of this promoted unsigned type. A smaller unsigned operand may promote to signed int: with eight-bit unsigned char and int able to represent 510, unsigned char c=255; c<<1 has type int and value 510, not 254. Negative shift counts or counts at least the promoted left operand's width cause undefined behavior. Signed left shifts require a nonnegative operand and a representable signed result under the C11/C17 rule; otherwise behavior is undefined. Right shift of a negative signed value is implementation-defined. Never silently trace −4>>1 as −2 without declaring an implementation.

A bit mask for position k can be 1u<<k only with k in range for unsigned int. Testing (flags & mask) != 0 checks whether a selected bit is set. flags & mask == 0 parses as flags & (mask == 0), because equality binds more tightly than bitwise AND. Parentheses express the intended grouping.

3. Grouping is not sequencing

Precedence and associativity determine the syntax tree. They do not generally decide which operand runs first. Ordinary + operands and function arguments have no left-to-right evaluation guarantee. In C11/C17, i++ + i and f(i++, i++) have unsequenced conflicting accesses to i and undefined behavior. Do not choose one of several numerical outputs.

Short-circuit operators do provide useful sequencing: && evaluates the left operand first and skips the right when left is zero; || skips the right when left is nonzero. The selected branch of ?: is evaluated after its condition, and only that branch is evaluated. The comma operator sequences its left expression before its right; a comma separating function arguments is not that operator. Separate full-expression statements also impose sequencing.

Worked safe trace: int i=0; int r=(i++ && ++i); gives i=1,r=0. The left operand yields the old value zero, increments i, and skips ++i. Changing && to & removes short-circuiting and introduces unsequenced modifications, so the changed expression has undefined behavior. A safe explicit alternative is int a=i++; int b=i; int r=a+b; with i initially 2: a=2,b=3,r=5,i=3.

Exam checklist and pitfalls

Annotate promoted types, apply conversions, check representability and shift bounds, then check sequencing before computing a value. Unsigned wrap is defined, signed overflow is undefined, and implementation-defined behavior requires a documented choice. Unspecified order without conflicting accesses need not be undefined: f(g(),h()) may call g and h in either order when they are independent. These categories are not interchangeable.

Original practice: six single-correct MCQs

These original teaching questions are not previous-year questions. Choose one answer.

P01. With CHAR_BIT=8 and int able to represent 0..65535, unsigned char a=250,b=10; what is a+b before assignment?

A. int 260 B. unsigned char 4 C. unsigned int 260 D. Undefined behavior

Answer: A

A: Both operands promote to int; the sum is 260.

B: Modulo 256 applies when converting back, not to this promoted sum.

C: int represents the entire unsigned-char range, so promotions choose int.

D: 260 is representable in the assumed int.

P02. What is the value of −1 < 1u in C11/C17?

A. 1 B. 0 C. Always implementation-defined D. Always undefined

Answer: B

A: The comparison is not performed in signed int.

B: −1 converts to UINT_MAX, which is greater than 1.

C: These operands yield a portable result without fixed width.

D: The unsigned conversion is defined.

P03. For unsigned int x=12u, what is x ^ 10u?

A. 8 B. 14 C. 6 D. 1

Answer: C

A: 8 is bitwise AND here.

B: 14 is bitwise OR here.

C: 1100 XOR 1010 is 0110.

D: 1 would be the logical truth value, not XOR.

P04. Which grouping does flags & mask == 0 have?

A. (flags & mask) == 0 B. flags & (mask == 0) C. (flags == mask) & 0 D. It is a syntax error

Answer: B

A: This desired bit test needs parentheses.

B: Equality binds more tightly than bitwise AND.

C: No precedence rule exchanges operands this way.

D: The expression is syntactically valid, though often unintended.

P05. Starting i=0, int r=(i++ && ++i); leaves:

A. i=2,r=1 B. i=1,r=1 C. Undefined behavior D. i=1,r=0

Answer: D

A: The right operand is skipped.

B: The left operand yields zero, so the result is zero.

C: Short-circuit sequencing makes this expression defined.

D: i++ yields zero and increments once; && skips ++i.

P06. Which fragment has undefined behavior in C11/C17 for initialized int i=2?

A. int r=i+3; B. int r=(i++,i); C. int r=i++ + i; D. int r=(i>0 ? i : 0);

Answer: C

A: This is a representable ordinary sum.

B: The comma operator sequences the increment before the read.

C: The modification and separate value read of i are unsequenced.

D: The conditional reads i without conflicting modification.

Sources and scope

GATE CS 2026 bounded extension of existing foundations. Not a whole-subject completion claim. Examples and questions are original.

Sign in to keep your progress. Sign in