473,387 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Conversion from Unsigned to Signed for bitwise.

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.

Nov 14 '05 #1
3 2974
co*******@gmail.com wrote:
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

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

Nov 14 '05 #2
On 3 Feb 2005 15:34:38 -0800, co*******@gmail.com wrote in
comp.lang.c:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #3
Thomas Matthews wrote:
co*******@gmail.com wrote:
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


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.


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)...
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


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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions....
4
by: Ken Tough | last post by:
Seems like a simple thing to find out, but I'm struggling. I have googled, but everything I find is about implicit conversion, not explicit. Is this implementation-specific, or does ANSI/ISO...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
14
by: junky_fellow | last post by:
Can anybody please explain this: When a value with integer type is converted to another integer type other than _Bool, if the new type is unsigned, the value is converted by repeatedly...
6
by: sarathy | last post by:
Hi, What is integer promotion? How is it different from arithmetic conversion? Regards, Sarathy
6
by: NM | last post by:
I am given an int and have to tell whether the bit representation of that int is a palindrome or not. Here is what I have come up with bool isIntPalindrome(int num) { unsigned int temp = num;...
6
by: subramanian | last post by:
Suppose I have the following statement: unsigned char x = 0; If I do, printf("%u\", ~x); it prints the value of UINT_MAX. I am using Intel machine. This same result is printed in both...
7
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are...
3
by: velpur | last post by:
Dear friends, ( Suppose we change byte orders. For example, our program reads a file as a character string and converts the byte order. ) // a simple code #define INTEL_CPU #ifdef...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.