473,394 Members | 1,694 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,394 software developers and data experts.

Shift operators.

If i'm shifting an integer 'n' times where n > sizeof(int), It's giving
the same value as
that of n-sizeof(n). Why is it behaving like this. I studied that the
bit 'll be replaced by '0'.

Example;

int i = 1;

printf ("%d\n", i<<32);

o/p: 1.

int i = 2;
printf ("%d\n", i<<33);

o/p: 2.

Jun 14 '06 #1
9 2580

deepak wrote:
If i'm shifting an integer 'n' times where n > sizeof(int), It's giving
the same value as
that of n-sizeof(n). Why is it behaving like this. I studied that the
bit 'll be replaced by '0'.

Example;

int i = 1;

printf ("%d\n", i<<32);

o/p: 1.

int i = 2;
printf ("%d\n", i<<33);

o/p: 2.


What compiler is giving you that output? I know GCC will happily
oblige you with the desired zeroes.

Tom

Jun 14 '06 #2
Le 14-06-2006, deepak <de*********@gmail.com> a écrit*:
If i'm shifting an integer 'n' times where n > sizeof(int), It's giving
the same value as
that of n-sizeof(n). Why is it behaving like this. I studied that the
bit 'll be replaced by '0'.


Yes, shifting for more than the type size is an undefined behavior.
I don't know why, but that is.

Marc Boyer
Jun 14 '06 #3
deepak wrote:
If i'm shifting an integer 'n' times where n > sizeof(int), It's giving
the same value as
that of n-sizeof(n). Why is it behaving like this.


Because it's allowed to, and its efficient on your implementation.

If the shift amount isn't in the range 0..bitswidthOfPromotedLeftOperand-1,
the result is undefined.

I suspect your implementation's machine only looks at the low 5 bits of
the shift amount.

--
Chris "it's as daft as a brush" Dollin
"Reaching out for mirrors hidden in the web." - Renaissance, /Running Hard/

Jun 14 '06 #4
Le 14-06-2006, to********@gmail.com <to********@gmail.com> a écrit*:

deepak wrote:
If i'm shifting an integer 'n' times where n > sizeof(int), It's giving
the same value as
that of n-sizeof(n). Why is it behaving like this. I studied that the
bit 'll be replaced by '0'.

Example;

int i = 1;

printf ("%d\n", i<<32);

o/p: 1.

int i = 2;
printf ("%d\n", i<<33);

o/p: 2.


What compiler is giving you that output? I know GCC will happily
oblige you with the desired zeroes.


Sure ?
-------------------- shift.c -------------------------
#include <limits.h>
#include <assert.h>
#include <stdio.h>

int main() {
assert( sizeof(int) == 4 && CHAR_BIT == 8 );
unsigned int taille = 32;
unsigned int r1 = 1u << taille;
printf("** %u\n", r1);
unsigned int r2 = 1u << 32;
printf("** %u\n", r2);
return 0;
}
-------------------- shift.c -------------------------

Run on a sparc

pogo% uname -a
SunOS pogo 5.9 Generic_118558-06 sun4u sparc SUNW,Sun-Blade-1000
pogo% gcc -v
gcc version 3.4.5
pogo% gcc shift.c ; ./a.out
shift.c: In function `main':
shift.c:10: warning: left shift count >= width of type
** 1
** 1

Run on intel

ubu> uname -a
Linux ubu 2.6.12-10-386 #1 Mon Feb 13 12:13:15 UTC 2006 i686 GNU/Linux
ubu> gcc -v
version gcc 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu9)
ubu> gcc shift.c ; ./a.out
shift.c: In function 'main':
shift.c:10: warning: left shift count >= width of type
** 1
** 0

Marc Boyer
Jun 14 '06 #5
Marc Boyer wrote:
Le 14-06-2006, deepak <de*********@gmail.com> a écritÂ*:
If i'm shifting an integer 'n' times where n > sizeof(int), It's giving
the same value as
that of n-sizeof(n). Why is it behaving like this. I studied that the
bit 'll be replaced by '0'.


Yes, shifting for more than the type size is an undefined behavior.
I don't know why, but that is.


Because the behaviour of the underlying machine shift instructions
differs in these circumstances, and so rather than mandating some result
and penalising machines with a different result, C allows the use of
the presumably-efficient instructions and doesn't define what happens
outside the "sensible" range.

--
Chris "not my choice" Dollin
"I'm still here and I'm holding the answers" - Karnataka, /Love and Affection/

Jun 14 '06 #6
Marc Boyer wrote:
Yes, shifting for more than the type size is an undefined behavior.


Shifting for greater than or equal to type width,
is undefined behavior.

--
pete
Jun 14 '06 #7
main()
{
int a = 1;
int count ;

printf ("Count:");
scanf ("%d", &count);

printf ("\no/p:%d ", a<<count);
}

/users/dejose->uname
SunOS
4 acura /users/dejose->./a.out
Count:32
o/p:1
Chris Dollin wrote:
deepak wrote:
If i'm shifting an integer 'n' times where n > sizeof(int), It's giving
the same value as
that of n-sizeof(n). Why is it behaving like this.


Because it's allowed to, and its efficient on your implementation.

If the shift amount isn't in the range 0..bitswidthOfPromotedLeftOperand-1,
the result is undefined.

I suspect your implementation's machine only looks at the low 5 bits of
the shift amount.

--
Chris "it's as daft as a brush" Dollin
"Reaching out for mirrors hidden in the web." - Renaissance, /Running Hard/


Jun 14 '06 #8
Was this (that deepak wrote):
main()
{
int a = 1;
int count ;

printf ("Count:");
scanf ("%d", &count);

printf ("\no/p:%d ", a<<count);
}

/users/dejose->uname
SunOS
4 acura /users/dejose->./a.out
Count:32
o/p:1


Supposed to be an answer to my:
I suspect your implementation's machine only looks at the low 5 bits of
the shift amount.


Because, if so, don't be so unkind as to top post, and actually
/say/ something about the relationship.

[It appears to me that your code/output is consistent with my
hypothesis.]

--
Chris "seeker" Dollin
A rock is not a fact. A rock is a rock.

Jun 14 '06 #9
Marc Boyer wrote:
Le 14-06-2006, deepak <de*********@gmail.com> a écrit :
If i'm shifting an integer 'n' times where n > sizeof(int), It's giving
the same value as
that of n-sizeof(n). Why is it behaving like this. I studied that the
bit 'll be replaced by '0'.


Yes, shifting for more than the type size is an undefined behavior.
I don't know why, but that is.

The compiler is obliged only to supply code which works where the
behavior is defined. The reason for the bigger shifts being undefined
is that the effect of the simple instruction sequence varies between
hardware implementations. It's likely the OP has found hardware where
the shifter in effect masks off the high order out-of-range bits.
Jun 14 '06 #10

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

Similar topics

12
by: DeepaK K C | last post by:
main() { int a =100; a = a>>32; printf(" %d", a); } it prints "a" as 100 only....but I am expecting a = 0..... can some one tell me the reason?
2
by: joshc | last post by:
I have a question about the following: 4. The result of E1 << E2 is E1 left-shifted E2 bit positions: vacated bits are filled with zeros. ... If E1 has a signed type and a nonnegative value, and...
43
by: Mehta Shailendrakumar | last post by:
Hello, Can anyone suggest me operator to perform arithmetic shift in C? May it be for a perticular compiler. Thank you in advance. Regards, Shailendra
5
by: ritesh.noronha | last post by:
K&R states that the right operand must be non-negative. "The shift operators << and >> perform left and right shifts of their left operand by the number of bit positions given by the right...
3
by: Simon Johnson | last post by:
Is there an inbuilt circular shift operator in c#? I've googled it but because c# happens to be musical note.. i get a large noise to signal ratio :P Simon.
8
by: Chua Wen Ching | last post by:
Hi, I had some beginner questions. Do we need Shift << >> or Logical AND OR XOR operator in our daily programming? I am not sure why i need to use it? I had some samples of c# codes using it. ...
7
by: Marty McFly | last post by:
Hello VB Gurus, I have an unusual requirement to shift an unsigned int right one bit: Dim myVar As UInt32 = UInt32.Parse("123456") Dim myResult As UInt32 myResult = myVar >> 1 However, the...
16
by: Santosh Nayak | last post by:
Hi, Is there any way to catch the losing bit occurring due to Right Shift Operator ? e.g int a = 5 ; a = a >1 ; // // a is now 2 and the least significant bit is lost // // I want this...
4
by: Felix Kater | last post by:
Hi, when I use something like int Shift= 3; long Value= 1 << Shift; What is the data type of the const value '1' here? In other terms: What is the possible maximum of 'Shift' here?
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: 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...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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.