473,847 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Shifting in C

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()
{
unsigned long x = 65;
unsigned long y = 0x3F;

y = y >x;

printf("%x\n",y );

return 1;
}
Jul 23 '08 #1
8 1560
manu said:
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()
{
unsigned long x = 65;
unsigned long y = 0x3F;

y = y >x;
If y has 65 or fewer bits (which it probably does), shifting 65 places
invokes undefined behaviour. Therefore any result, or none, is admissible
as far as implementation conformance is concerned.

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

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 23 '08 #2
On 23 Jul, 08:08, manu <manumg...@gmai l.comwrote:
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()
{
* * * * unsigned long x = 65;
unsigned long y = 0x3F;

* * * * y = y >x;
note C has a short version of this

y >>= x;

(someone else answered your actual question)
>
* * * * printf("%x\n",y );
* * * * return 1;
}
--
Nick Keighley
Jul 23 '08 #3
manu wrote:
I have executed the below program and got 0x1f as output...
Can anyone explain me why this output is coming instead of zero?
Because the result of the shift operator is undefined when the value of
the right operand is greater than or equal to the width in bits of the
left operand. You attempt to shift an unsigned long by 65, but this has
a defined meaning only if sizeof(unsigned long)*CHAR_BIT is at least 66,
which appears not to be true for your implementation.

Additionally:

Here you need #include <stdio.h>. Omitting a prototype for a variadic
function (printf) is an error.
int main()
{
unsigned long x = 65;
unsigned long y = 0x3F;

y = y >x;

printf("%x\n",y );
^^
"%x" is a specifier for an unsigned int, not for an unsigned long.
"%x" is an error; it should be "%lx".
>
return 1;
^^^
The only value with defined meaning for this return when you don't
include <stdlib.his 0. If you #include <stdlib.hyou gain
EXIT_FAILURE and EXIT_SUCCESS, making 3 values with defined meaning, and
none of them is 1.
}
Jul 23 '08 #4
On Jul 23, 1:13*pm, Martin Ambuhl <mamb...@earthl ink.netwrote:
manu wrote:
I have executed the below program and got 0x1f as output...
Can anyone explain me why this output is coming instead of zero?

Because the result of the shift operator is undefined when the value of
the right operand is greater than or equal to the width in bits of the
left operand. You attempt to shift an unsigned long by 65, but this has
a defined meaning only if sizeof(unsigned long)*CHAR_BIT is at least 66,
which appears not to be true for your implementation.

Additionally:

* *Here you need #include <stdio.h>. *Omitting a prototype for a variadic
* *function (printf) is an error.
int main()
{
* *unsigned long x = 65;
unsigned * long y = 0x3F;
* *y = y >x;
* *printf("%x\n", y);

* * * * * * * * *^^
* *"%x" is a specifier for an unsigned int, not for an unsigned long.
* *"%x" is an error; it should be "%lx".
* *return 1;

* * * * * * * *^^^
* *The only value with defined meaning for this return when you don't
include <stdlib.his 0. *If you #include <stdlib.hyou gain
EXIT_FAILURE and EXIT_SUCCESS, making 3 values with defined meaning, and
none of them is 1.
}

Thanks Richard,Nick and Martin for valuable information..
Jul 23 '08 #5
In article <H9************ *************** ***@bt.com>,
Richard Heathfield <rj*@see.sig.in validwrote:
>manu said:
>unsigned long x = 65;
unsigned long y = 0x3F;
>y = y >x;
>If y has 65 or fewer bits (which it probably does), shifting 65 places
invokes undefined behaviour. Therefore any result, or none, is admissible
as far as implementation conformance is concerned.
Additional information: at least one common processor family takes the
shift code modulo 32 when shifting 32 bit numbers. That's an
implementation detail and perfect permitted by the standard section
that Richard cited; just thought you might want to know how you
ended up with the particular answer you did.
--
"Ignorance has been our king... he sits unchallenged on the throne of
Man. His dynasty is age-old. His right to rule is now considered
legitimate. Past sages have affirmed it. They did nothing to unseat
him." -- Walter M Miller, Jr
Jul 23 '08 #6
Bet GCC masked x (and or mod) so shift would be <= of bits in y. If y had less than 65 bits then the shift would be 1 ie a result of 0x1f.

w..

manu wrote:
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()
{
unsigned long x = 65;
unsigned long y = 0x3F;

y = y >x;

printf("%x\n",y );

return 1;
}
Jul 23 '08 #7
On Wed, 23 Jul 2008 15:02:08 +0000 (UTC), ro******@ibd.nr c-cnrc.gc.ca
(Walter Roberson) wrote in comp.lang.c:
In article <H9************ *************** ***@bt.com>,
Richard Heathfield <rj*@see.sig.in validwrote:
manu said:
unsigned long x = 65;
unsigned long y = 0x3F;
y = y >x;
If y has 65 or fewer bits (which it probably does), shifting 65 places
invokes undefined behaviour. Therefore any result, or none, is admissible
as far as implementation conformance is concerned.

Additional information: at least one common processor family takes the
shift code modulo 32 when shifting 32 bit numbers. That's an
implementation detail and perfect permitted by the standard section
that Richard cited; just thought you might want to know how you
ended up with the particular answer you did.
What particular answer did he wind up with? He most certainly did not
tell us. And regardless of the undefined behavior caused by the shift
itself, the program produces more undefined behavior by passing "%x"
as a conversion specifier to printf() with a type other than unsigned
int.

--
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
Jul 24 '08 #8
Jack Klein said:
On Wed, 23 Jul 2008 15:02:08 +0000 (UTC), ro******@ibd.nr c-cnrc.gc.ca
(Walter Roberson) wrote in comp.lang.c:
<snip>
>
>just thought you might want to know how you
ended up with the particular answer you did.

What particular answer did he wind up with?
0x1f
He most certainly did not tell us.
Er, actually, he most certainly did. See the OP.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 24 '08 #9

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

Similar topics

6
4414
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)
17
1594
by: Jack | last post by:
Hi, This is a strange problem I am encountering. I have a asp page with a confirmation.asp page that saves data to a table. There are few text fields that are captured by the confirmation page as l_f_name = Request.Form("txt_Name") l_f_personstitle = Request.Form("txt_Title") l_f_PhoneAreaCode = Request.Form("txt_PhoneAreaCode") l_f_Phone1 = Request.Form("txt_Phone1") l_f_Phone2 = Request.Form("txt_Phone2") l_f_Date =...
9
8791
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;
8
3675
by: ben | last post by:
i have a bit of code, that works absolutely fine as is, but seems over complicated/long winded. is there anyway to shorten/simplify it? the code is below. description of it: it's like strcpy in that it copies one block of data to another block of data until the block that is being copied contains a zero/null. the difference with this code is that it's doing 4bits at a time (all the values are 4bits) and the two blocks of data may not be...
2
2072
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
10730
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;
20
2528
by: Charles Sullivan | last post by:
I understand different processor hardware may store the bits in a byte in different order. Does it make a difference in C insofar as bit-shifting unsigned char variables is concerned? E.g, if I have unsigned char x = 1; is it always true that (x << 1) == 2 (x << 2) == 4 etc?
16
1565
by: lak | last post by:
i know left and right shift normally,but i cant know what happens if it is negative. for example int x=-2; x<<=1;//what happens here
4
1873
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...
12
2260
by: Boltar | last post by:
I seem to be having yet more wierd issue with bit shifting. It seems the following code doesnt do anything under gcc (ie it returns -1 as both results). Anyone know why? Is it another language definition or CPU issue? main() { printf("%d\n",(int)0xFFFFFFFF >1); printf("%d\n",(int)-1 >1); }
0
10658
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...
1
10718
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9497
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
7889
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
7062
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5730
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5915
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4541
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
3
3171
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.