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

Does bit shift depend on platform/implementation?

Hello
On http://cplus.about.com/od/advancedtu.../aa042203h.htm
I found an example of bitshifting (see below).
Q: Is the bitshifting dependant on platform specific things (like how
the number is represented in memory) or can one count on the usual
behaviour (eg "<< 1" multiplies by 2) on any platform?

Thanks Phil

-- code found on web site --
#include <iostream>
using namespace std;

int main()
{
unsigned short bvec = 1024;

for (int i = 0; i < 15; i++) {
cout << bvec << endl;
bvec = bvec >1;
}

return 0;
}
----

Results:
1024
512
256
128
64
32
16
8
4
2
1
0
0
0
0
Aug 2 '06 #1
1 1711
Philipp wrote:
On http://cplus.about.com/od/advancedtu.../aa042203h.htm
I found an example of bitshifting (see below).
Q: Is the bitshifting dependant on platform specific things (like how
the number is represented in memory) or can one count on the usual
behaviour (eg "<< 1" multiplies by 2) on any platform?
Yes, the behaviuor is standard. Left shift by 1 multiplies by 2, and
so on. The only implementation-defined behaviour is for the right
shift of a negative value of a signed type.

Of course, shifting more positions than the bits in the left operand
(integral-promoted) has undefined behaviour, also shifting with the
right operand negative also has undefined behaviour.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 2 '06 #2

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

Similar topics

43
by: Minti | last post by:
Hi there everybody, I was just wondering that too many people choose to use language like Java because of its architecture independence, this AI is achieved because Java is as such a...
11
by: Vinod Patel | last post by:
I have a piece of code : - void *data; ...... /* data initialized */ ...... struct known_struct *var = (struct known_struct*) data; /*typecasting*/ How is this different from simple...
35
by: Sunil | last post by:
Hi all, I am using gcc compiler in linux.I compiled a small program int main() { printf("char : %d\n",sizeof(char)); printf("unsigned char : ...
8
by: rupert | last post by:
It seems a need a paradime shift in understanding how C++ use libraries. Sun keeps is together under their API. And Javadoc enables a neat way of displaying functions of a local program to display...
56
by: Christian Christmann | last post by:
Hi, in the header of my class I've a constant static const int a = ( 1 << 32 ) - 1; When compiling the code, g++ issues the warning "warning: left shift count >= width of type" Why? And...
13
by: sunny | last post by:
Hi All does the output of this command depend on the compiler. int val=1234; int* ptr=&val; printf("%d %d %d",val,*ptr++,++*ptr); O/p i was expecting 1234,1234,1235.
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
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...
3
by: c.lang.myself | last post by:
#include <stdio.h> float puzzle( float inp ) { const float ths = 1.5F; const long k = 21*76069667; float a, b; int c;
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?
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
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
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...
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...
0
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,...

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.