Conversion from Unsigned to Signed for bitwise.

C

codefixer

Hi,

I was wondering what will be the output for this piece of code. I am in
confusion regarding the promotions of bitfield. If your reply is to
compile, execute and check out myself, Thank you please don't read any
further.

Please don't execute the code. I want to know the results according to
the standards.
From ISO/IEC 9899:1999 (E) Section 6.3.1.1

If an int can represent all values of the original type, the value is
converted to an int; otherwise, it is converted to an unsigned int.
These are called the integer promotions.48) All other types are
unchanged by the integer promotions.

For the following code,
Do you think the program must output "Inside bitfield promoted to
signed int"

#include <stdio.h>

Struct Temp
{
unsigned int a:4;
unsigned int b:32;
};

int main(void)
{

struct Temp s;
s.a = 0;
s.b = 0;
if (s.a - 5 < 0)
printf(" Inside bitfield promoted to signed int\n");
else
printf("Inside bitfield promoted to unsigned int\n");

return 0;
}


Thank you for the time and patience.
 
T

Thomas Matthews

Hi,

I was wondering what will be the output for this piece of code. I am in
confusion regarding the promotions of bitfield. If your reply is to
compile, execute and check out myself, Thank you please don't read any
further.

Please don't execute the code. I want to know the results according to
the standards.


If an int can represent all values of the original type, the value is
converted to an int; otherwise, it is converted to an unsigned int.
These are called the integer promotions.48) All other types are
unchanged by the integer promotions.

For the following code,
Do you think the program must output "Inside bitfield promoted to
signed int"

#include <stdio.h>

Struct Temp
Syntax error. According the specification the language is
case sensitive. The keyword is "struct", any other spellings
indicate an identifier.

{
unsigned int a:4;
unsigned int b:32;
};
Note that there are 36 bits in this structure. Is this what
you wanted? Most processors are in powers of 2, but there
are those that aren't.

int main(void)
{

struct Temp s;
s.a = 0;
s.b = 0;
if (s.a - 5 < 0)
printf(" Inside bitfield promoted to signed int\n");
else
printf("Inside bitfield promoted to unsigned int\n");

return 0;
}


Thank you for the time and patience.

My understanding is that the expression "s.a - 5"
will result in an underflow or undefined behavior.
On many systems, the expression will "wrap around",
but still be an unsigned integer. I don't believe
that the expression will be promoted to a signed
integer.

Also, comparing an unsigned value to less than zero
should generate a warning by a decent (nice) compiler.

Here is my reasoning:
4 bits has a range of zero to 15.
In binary:
X 0000 --> Imply borrow --> 1 0000
- 0 0101 --> --> - 0 0101
======== ========
0 1010 0 1010

In this case, the high order bit is set possibly
indicating a negative number. But the variable
is unsigned, so all bits are used for data (none
for sign).

Thus:
s.a - 5 == 0 - 5 == 10 (remainder from underflow)

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
J

Jack Klein

Hi,

I was wondering what will be the output for this piece of code. I am in
confusion regarding the promotions of bitfield. If your reply is to
compile, execute and check out myself, Thank you please don't read any
further.

No, don't do that. Too many compilers get it wrong.

There is a long and ongoing thread in this group titled "Bit-fields
and integral promotion" started by Carsten Hansen on January 28. Go
read it, and if you have anything else to ask or say about this issue,
add it to that thread.
 
M

Michael Mair

Thomas said:
Syntax error. According the specification the language is
case sensitive. The keyword is "struct", any other spellings
indicate an identifier.



Note that there are 36 bits in this structure. Is this what
you wanted? Most processors are in powers of 2, but there
are those that aren't.

Read up on bitfields. You are spreading misinformation.
a and b could be stored in different bytes even if both would
take <=CHAR_BIT/2 bits, they could be stored in arbitrary
order and many other things. I'd rather wonder about the 32
in the sense that unsigned int as a type may well have less
than 32 bit (16 suffice)...
My understanding is that the expression "s.a - 5"
will result in an underflow or undefined behavior.
On many systems, the expression will "wrap around",
but still be an unsigned integer. I don't believe
that the expression will be promoted to a signed
integer.

Also, comparing an unsigned value to less than zero
should generate a warning by a decent (nice) compiler.

Here is my reasoning:
4 bits has a range of zero to 15.
In binary:
X 0000 --> Imply borrow --> 1 0000
- 0 0101 --> --> - 0 0101
======== ========
0 1010 0 1010

You want to arrive at "0 1011" (2**4-5=11).
In this case, the high order bit is set possibly
indicating a negative number. But the variable
is unsigned, so all bits are used for data (none
for sign).

Thus:
s.a - 5 == 0 - 5 == 10 (remainder from underflow)

This is not correct. If we were talking about s.a-=5,
this might be correct.
We have a really long very recent thread about this,
as Jack Klein pointed out. Read it.
Essentially, it comes down to "bitfields are promoted
unsigned-preserving" vs. "bitfields are promoted
value-preserving" (value-preserving in the sense that
the standard integer promotions hold).


Cheers
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top