473,698 Members | 2,086 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bit shifting past length of type

Is it a defined operation when I do something like this:
char a = 15;
a = a<<9;
Oct 2 '08
12 2100
On Wed, 1 Oct 2008 23:43:12 -0700 (PDT), Peter Nilsson
<ai***@acay.com .auwrote in comp.lang.c:
Richard Heathfield <r...@see.sig.i nvalidwrote:
British0zzy said:
Is it a defined operation when I do something like
this:
char a = 15;
a = a<<9;
Only on systems where char has at least 10 bits.

The relevant Standard cite is 3.3.7 Bitwise shift
operators:

"If the value of the right operand is negative or is
greater than or equal to the width in bits of the
promoted left operand, the behavior is undefined."
^^^^^^^^

That is almost incidental. The promoted type must have
a width of at least 16.

The problem is that plain char may be signed or
otherwise promote to a narrow int.
What exactly do you mean by "promote to a narrower int"? How can int
be narrower than int?

Did you mean "promote to a narrower integer type than int"? If so,
chapter and verse, please.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Oct 3 '08 #11
Peter Nilsson wrote:
Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
>>>British0zz y said:
char a = 15;
a = a<<9;
...The value of CHAR_BIT affects the outcome because
it affects the value of CHAR_MAX used in the conversion,
but it does not affect the operation of or validity of
the shift.

... changing the second line to `int i = a << 9;'
yields a fragment whose behavior is the same on all
implementation s.

If CHAR_MAX <= INT_MAX and a (INT_MAX >9), then the
behaviour of a << 9 is undefined.
Yes. But since INT_MAX is at least 32767, then
INT_MAX>>9 is at least 63, a number greater than the
value of `a'. The second half of your premise never
holds, so the conclusion does not apply.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Oct 3 '08 #12
Jack Klein <jackkl...@spam cop.netwrote:
Peter Nilsson <ai...@acay.com .auwrote in comp.lang.c:
Richard Heathfield <r...@see.sig.i nvalidwrote:
British0zzy said:
Is it a defined operation when I do something like
this:
char a = 15;
a = a<<9;
>
Only on systems where char has at least 10 bits.
>
The relevant Standard cite is 3.3.7 Bitwise shift
operators:
>
"If the value of the right operand is negative or is
greater than or equal to the width in bits of the
promoted left operand, the behavior is undefined."
* ^^^^^^^^
That is almost incidental. The promoted type must have
a width of at least 16.

The problem is that plain char may be signed or
otherwise promote to a narrow int.

What exactly do you mean by "promote to a narrower int"?
Since I didn't write that, I can't answer you. :-)

What I mean by 'narrow int' is that if plain char promotes
to int, then int must be at least 9 bits wider than plain
char for a left shift of 9 bits to be guaranteed to work
work for arbitrary char values. [Obviously, there is still
the problem of assigning the shifted value back to char.]

--
Peter
Oct 3 '08 #13

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

Similar topics

6
4402
by: David Stockwell | last post by:
Hi, My background is c/c++ and java. I'm learning python at this point. My question is does python share java's peculiar mode of bit shifting, or does python adhere closer to c's bit shifting? in java there are 3 kinds of bit shifts: << (shift left) >> (preserve the sign bit as we move right ) >>> (0 filled on the left as we move right)
9
8742
by: GGG | last post by:
Noticed something odd in the way bit shifting was working today. As far as I have ever heard, shifting will shift in zeros(signed ints aside) However I foudn something odd when I am shifting exactly the number of bits in a value... i.e. uint32_t x = 5;
0
1682
by: Rick Slansky | last post by:
Hi: This should be a fairly simple page, which lines up the second row cell nicely in the lower right corner. It does in Netscape, but when I check the page in Explorer, that cell (the entire cell, not just the contents) is shifted down by 6 to 8 pixels. I would appreciate any help in identifying why the alignment difference is happening with Explorer.
8
1650
by: Nick Patavalis | last post by:
Ok, what is this supposed to print: #include <stdio.h> #include <stdint.h> int main (void) { uint32_t r, s;
20
15186
by: Xenos | last post by:
Can anyone tell me what the standard says about using fseek (on a binary file) to seek past the end-of-file? I can't find anything in my (draft) copy of the standard, nor in the FAQ. Thanks
2
2058
by: salsipius | last post by:
Can someone please help me clarify the below code. I think the shifting has to do with converting datatypes and/or loss of data but am not really clear on the details, could you help shed some light please? // Allocate array for( i = 0; i < Length; i++ ) { //pArray_00 is a BYTE Array -- Here a cast is used because
10
10691
by: krunalb | last post by:
Hi, I am trying to shift unsigned long long value by 64 bits and this is what i get #include <stdio.h> int main() { unsigned short shiftby= 64;
4
1866
by: Neil | last post by:
I previously posted about data shifting between records in my Access 2000 MDB with a SQL Server 7 back end, using ODBC linked tables. Every once in a while, data from one record mysteriously appears in another record. This incident happened again, this time adding a new wrinkle to the situation. There are two tables -- TableA and TableB -- which have a one-to-one relationship with each other, joined on TableA's autonumber primary key...
8
1547
by: manu | last post by:
Hi All, I have executed the below program and got 0x1f as output... Can anyone explain me why this output is coming instead of zero? Compiler - gcc int main() {
0
9157
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9028
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7728
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2330
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.