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;
} 8 1547
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
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
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.
}
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..
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
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;
}
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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)
|
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 =...
|
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;
|
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...
|
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
| |
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;
|
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?
|
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
|
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...
|
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);
}
|
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...
| |
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,...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |