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

right shifting

if I have the foolowing code,

main()
{
unsigned int i=34;
i>>=32;
}

why does the value of i remain 34? I thought it should be a zero.
(the g++ compiler gives me a warning about shifting >= size of variable.)
Nov 9 '06 #1
2 1896
crookie wrote:
if I have the foolowing code,

main()
{
unsigned int i=34;
i>>=32;
}

why does the value of i remain 34? I thought it should be a zero.
(the g++ compiler gives me a warning about shifting >= size of variable.)
The standard [5.8/1] says about the shift operators:

[...] The behavior is undefined if the right operand is negative, or
greater than or equal to the length in bits of the promoted left operand.

It would appear that on your platform sizeof(int)<=32, whence you have
undefined behavior: anything could happen.
Best

Kai-Uwe Bux
Nov 9 '06 #2
crookie wrote:
if I have the foolowing code,

main()
{
unsigned int i=34;
i>>=32;
}

why does the value of i remain 34? I thought it should be a zero.
(the g++ compiler gives me a warning about shifting >= size of variable.)
Because on your system, an unsigned int has 32 or fewer bits, and (as I
recently learned) shifting by any number greater than or equal to the
number of bits is Undefined Behavior. Which means that any value of i
is equally valid.

For more detail, see this thread from over the summer:

http://tinyurl.com/y382v3

Best regards,

Tom

Nov 9 '06 #3

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

Similar topics

17
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...
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...
8
by: Nick Patavalis | last post by:
Ok, what is this supposed to print: #include <stdio.h> #include <stdint.h> int main (void) { uint32_t r, s;
2
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...
1
by: cricketunes | last post by:
Hey folks I am implementing the Huffman encoding algorithm. I have created the tree and its perfect. Now while searching for a node value, I need to write a 0 to the encoded file if I am moving...
18
by: woessner | last post by:
Does anyone know of a fast way to multiply floating point numbers by powers of two? Conceptually, all you need to do is add to the mantissa. But can I write C code (or x86 assembly) to accomplish...
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...
4
by: G Iveco | last post by:
I am using this type of code to do right-shifting, B = 3; data1 = (data + (1 << (B-1))) >B; data1 seems incorrect when data = -4-8*i.. which means it rounds -1.5 to -1 instead of -2. On...
4
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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...

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.