473,406 Members | 2,371 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,406 software developers and data experts.

About shift one number more than 32.

L.G
I met one problem of shift a number with equal or more than 32 bits.

When running this :

uint32_t num = 0x45F5F;
int shift = 33;
uint32_t res = num << shift;

printf( "\\exp: %08XH << %d = %08XH\n", num, shift, res );

It shows :

\exp: 00045F5FH << 33 = 0008BEBEH

Is it personal ?

My environment :
WinXP SP2 + Cygwin 2.510.2.2

Regards.

-L.Guo

May 31 '07 #1
7 2455
L.G wrote:
I met one problem of shift a number with equal or more than 32 bits.

When running this :

uint32_t num = 0x45F5F;
int shift = 33;
uint32_t res = num << shift;

printf( "\\exp: %08XH << %d = %08XH\n", num, shift, res );

It shows :

\exp: 00045F5FH << 33 = 0008BEBEH
What did you expect?

--
Ian Collins.
May 31 '07 #2
L.G
On 5 31 , 10 23 , Ian Collins <ian-n...@hotmail.comwrote:
>
What did you expect?

--
Ian Collins.
Commonly, left shift one bit should fill a zero at the lowest bit.

So, if I shift left some more bits, why not fill corresponding zeros ?

-L.G

May 31 '07 #3
L.G wrote:
On 5 31 , 10 23 , Ian Collins <ian-n...@hotmail.comwrote:
>What did you expect?
Please don't quote signatures.
>
Commonly, left shift one bit should fill a zero at the lowest bit.

So, if I shift left some more bits, why not fill corresponding zeros ?
Typical shifters on 32 bit machines use modulo 32, so a shift of 33 bits
is equivalent to a shift of 1 bit.

--
Ian Collins.
May 31 '07 #4
"L.G" wrote:
Ian Collins <ian-n...@hotmail.comwrote:
>>
What did you expect?

Commonly, left shift one bit should fill a zero at the lowest bit.
So, if I shift left some more bits, why not fill corresponding zeros ?
But you only shifted one bit. Read the manuals.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 31 '07 #5
"L.G" <le*********@gmail.comwrites:
I met one problem of shift a number with equal or more than 32 bits.

When running this :

uint32_t num = 0x45F5F;
int shift = 33;
uint32_t res = num << shift;

printf( "\\exp: %08XH << %d = %08XH\n", num, shift, res );

It shows :

\exp: 00045F5FH << 33 = 0008BEBEH

Is it personal ?
Personal? I have no idea what you mean by that, but ...

The definition of the shift operators says in part (C99 6.5.7p3):

The integer promotions are performed on each of the operands. The
type of the result is that of the promoted left operand. If the
value of the right operand is negative or is greater than or equal
to the width of the promoted left operand, the behavior is
undefined.

So don't do that.

Practically speaking, different CPUs behave differently for operations
like this. Leaving such cases undefined allows the common case
(0 <= bits < WIDTH) to be implemented straightforwardly without having
to emit extra code to handle other cases.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 31 '07 #6
L.G
On 5ÔÂ31ÈÕ, ÉÏÎç11ʱ28·Ö, Keith Thompson <k...@mib.orgwrote:
The definition of the shift operators says in part (C99 6.5.7p3):

The integer promotions are performed on each of the operands. The
type of the result is that of the promoted left operand. If the
value of the right operand is negative or is greater than or equal
to the width of the promoted left operand, the behavior is
undefined.

So don't do that.

Practically speaking, different CPUs behave differently for operations
like this. Leaving such cases undefined allows the common case
(0 <= bits < WIDTH) to be implemented straightforwardly without having
to emit extra code to handle other cases.
Thank you.

I'll follow this in my future work.

May 31 '07 #7
"L.G" wrote:
>
I met one problem of shift a number with equal or more than 32 bits.

When running this :

uint32_t num = 0x45F5F;
int shift = 33;
uint32_t res = num << shift;

printf( "\\exp: %08XH << %d = %08XH\n", num, shift, res );

It shows :

\exp: 00045F5FH << 33 = 0008BEBEH

Is it personal ?

My environment :
WinXP SP2 + Cygwin 2.510.2.2
My understanding is that shifting by more that the width of the
item is either "implementation defined" or UB. In this case,
you are asking to shift a 32-bit integer by 33 bits.

My guess, based on experience with x86 hardware, is that the CPU
only uses the low-order 5 bits of the shift value (2^5 == 32),
so a shift of 33 (0x21) is treated the same as a shift of 1
(0x21 & 0x1f == 0x01).

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
May 31 '07 #8

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

Similar topics

9
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...
4
by: Michael Fay | last post by:
There have been threads in this newsgroup explaining how to tab out of a subform to the parent form -- without having to use ctrl-tab (of which users might be unaware -- and at any rate, they...
10
by: chanma | last post by:
code1:var x=0xf0000000; alert(x); output:4026531840 code2:var b=0xf<<28; alert(b); output:-268435456
36
by: utab | last post by:
Dear, I have experince in C( numerical projects, like engineering problems, scientific applications) I have the basic notion of C++ also, I have read Accelerated C++ until Chapter 7, however it...
12
by: Mick_fae_Glesga | last post by:
OK, the solution to this is probably blindingly obvious to everyone, but... surely it can't be right. I am compiling with borland bcc32 free compiler this piece of code is designed to identify...
24
by: Nishu | last post by:
Hi All, Could you please explain whether C standard supports logical right shift operation using some operator? I know somewhere I read about >>operator. I thought, it is in C but i think i'm...
7
by: Bint | last post by:
Hi, What is a simple way to shift the elements in an array, circularly? Is there a way to do it so that you don't need a separate storage array? IE I want to shift array A{1,2,3,4,5,6,7,8} by...
1
by: pantagruel | last post by:
Hi, I have an array like the following: for(x=0;x<results.length;x++){ alert(results.length); extracted=results.shift(); alert(results.length); if(results.indexOf(extracted)== -1){
7
by: beginers of c | last post by:
i have a binary number 1001 .i have to left shift it to 1 position like 0011.if their any datatype or code is available to shift the binary numbers
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...

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.