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. 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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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....
|
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...
|
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. ...
|
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...
|
by: sarathy |
last post by:
Hi,
What is integer promotion? How is it different from
arithmetic conversion?
Regards,
Sarathy
|
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;...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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...
| |