Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!alice!dutoit!dmr From: dmr@dutoit.UUCP Newsgroups: net.lang.c Subject: conversion of short to unsigned int Message-ID: <2026@dutoit.UUCP> Date: Wed, 27-Mar-85 00:25:47 EST Article-I.D.: dutoit.2026 Posted: Wed Mar 27 00:25:47 1985 Date-Received: Thu, 28-Mar-85 00:16:25 EST Lines: 31 Mike Wescott wondered why, after unsigned short us = -3; short s = -3; the comparison (unsigned int)s == us should yield true (this, evidently, on a VAX). The reason is that his compiler, along with those of a great many people, has a bug. The short s should indeed promote to 0xfffffffd (an int) and then be cast to unsigned (same bits, in 2's complement), and compare unequal with the 0xfffd stored in us . This is another instance where a reasonably clear, if complicated, description in the manual appears hopelessly confused because the compiler doesn't implement the manual. Incidentally, Ken Turkowski's remarks about casts: "... a cast, saying that s is considered unsigned rather than signed. It [a cast] is NOT a conversion" are quite wrong. Casts specify conversions, not requests to reinterpret a variable as containing some type other than its own. Many casts, especially those involving unsigned, or pointers, do indeed not actually change any bits in the value. This fact may have misled Ken, and probably also the compiler writer. The way to think of casts is to imagine assigning their operands to temporary variables with the type specified by the cast, and then using the temporary in the larger expression in which the cast occurs. Dennis Ritchie