473,750 Members | 2,265 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is the value of (-1 >> 6)?


Are the bitwise operators >>, <<, &, |, ~, ^ only defined for
non-negative values?

The little program below produces the output
3 255
I expected to get the 3 but why does it produce the 255 when the
variable c is not typecast first?


#include <stdio.h>

int main(int argc, char **argv) {

char c = -1;
unsigned char uc1 = (unsigned char)c >> 6;
unsigned char uc2 = c >> 6;

printf("%hhu %hhu\n", uc1, uc2);
return 0;
}
Nov 13 '05 #1
5 1751
Grant wrote:

Are the bitwise operators >>, <<, &, |, ~, ^ only defined for
non-negative values?

The little program below produces the output
3 255
I expected to get the 3 but why does it produce the 255 when the
variable c is not typecast first?


#include <stdio.h>

int main(int argc, char **argv) {

char c = -1;
unsigned char uc1 = (unsigned char)c >> 6;
unsigned char uc2 = c >> 6;

printf("%hhu %hhu\n", uc1, uc2);
return 0;
}

When you don't specify whether you want signed or unsigned char, you leave
it up to the designers of your compiler. Apparently, you tested on a
system where it's signed 2's complement.
--
Tim Prince
Nov 13 '05 #2
Grant wrote:

Are the bitwise operators >>, <<, &, |, ~, ^ only defined for
non-negative values?


Yes.

--
pete
Nov 13 '05 #3
On Sat, 11 Oct 2003 02:29:51 GMT, pete <pf*****@mindsp ring.com> wrote
in comp.lang.c:
Grant wrote:

Are the bitwise operators >>, <<, &, |, ~, ^ only defined for
non-negative values?


Yes.


NO!

They are only well-defined. Their effects when right shifting signed
types is implementation-defined, but that is a far cry from undefined.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #4
Jack Klein wrote:

On Sat, 11 Oct 2003 02:29:51 GMT, pete <pf*****@mindsp ring.com> wrote
in comp.lang.c:
Grant wrote:

Are the bitwise operators >>, <<, &, |, ~, ^ only defined for
non-negative values?


Yes.


NO!

They are only well-defined. Their effects when right shifting signed
types is implementation-defined, but that is a far cry from undefined.


"undefined aspects" is undefined, enough for me.

N869
6.5 Expressions
[#4]
... bitwise operators ...
These operators return values that depend on the internal
representations of integers, and have implementation-defined
and undefined aspects for signed types.

--
pete
Nov 13 '05 #5
pete wrote:

Jack Klein wrote:

On Sat, 11 Oct 2003 02:29:51 GMT, pete <pf*****@mindsp ring.com> wrote
in comp.lang.c:
Grant wrote:
>
> Are the bitwise operators >>, <<, &, |, ~, ^ only defined for
> non-negative values?

Yes.
NO!

They are only well-defined.
Their effects when right shifting signed
types is implementation-defined,
but that is a far cry from undefined.


right shift *is* implementation defined,
but OP asked about bitwise operators as a group,
so I took my answer from the part of the standard
which addresses the issue for bitwise operators
as a group.

"undefined aspects" is undefined, enough for me.

N869
6.5 Expressions
[#4]
... bitwise operators ...
These operators return values that depend on the internal
representations of integers, and have implementation-defined
and undefined aspects for signed types.


--
pete
Nov 13 '05 #6

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

Similar topics

2
5233
by: Guinness Mann | last post by:
Greetings, I asked this question over on the ADO.NET newsgroup and couldn't scrape up an answer. I realize that it is more of an ADO question than an SQL Server question, but I'm hoping there might be an ADO programmer here that can explain this to me. I'm developing database applications using C# on VS.NET 2003 and SQL Server Standard edition (SP3a).
0
1423
by: Ly Lam | last post by:
In my .resx file (below) I wish to reference an external bitmap that is located in my project's folder. When I try to compile the project I get an error on my .resx file: Resource transformation for file 'Images.resX' failed. Could not find file "C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE\TEditButton.bmp". It seems like the resource compiler has a base-path set to the vs.net's IDE directory. How do I specify a...
4
3887
by: Edward Diener | last post by:
I really do not understand the difference between the <summary> and <value> XML document tags for a property. Why would one use one as opposed to the other for a property, and when would one use both ?
13
1554
by: Jim in Arizona | last post by:
I made a page with a gridview that has rows show a different color if a number in a column is greater than or equal to 45. I also did this conditional formatting for the column next to it. Here's my code. in the aspx file =============================== <asp:GridView ID="gvData" runat="server" OnRowDataBound="doColor"> =============================== in the aspx.vb file
8
4724
by: A. Farber | last post by:
Hello, I have this simple program: #include <stdio.h> #include <stdlib.h> int main() { char *args = "2162508224"; printf("args=%s, atoi=%lu, atol=%lu\n",
1
1777
by: Q | last post by:
Question: For our GPS application I need to convert the minutes part (heximal value) to a decimal/floating point value. So: 41609218 (4 bytes) has to be converted to: 14.034 (approx) (format of the float is IEEE-754 ) Thanx in advance for any help!!!!
4
10341
by: magix | last post by:
Hi, in my SELECT (like below), the option value has 2 values, separated with comma "," <select name="sIdName"> <option value="10,John">John</option> <option value="11,Eva">Eva</option> <option value="22,Danny">Danny</option> </select>
3
2110
by: ABeginner | last post by:
I want to give validation rule for a column - I want one field value must be greater than another field value . both column's are in same table can anybody help me???
4
1896
Fary4u
by: Fary4u | last post by:
Hi for the last few months i've just stuck in 1 error & i don't know how to figer it out could any body find out where is the problem gonna be ? it's working fine but when u add product into basket it's bring automatically 1 extra product some time 2 or some time it's working fine i think so problem with GLOBAL.asa File that's not generating proper session variable GLOBAL.asa Sub Application_OnStart
3
2396
by: Christof Warlich | last post by:
Hi all, in the following example, Index<unsigned int x>::value allows to calculate the "rounded up" index from any "increasing" value during compile time. Unfortunately, the definition of the index - value pairs does not really look nice. The definition through an array would look much nicer, but does not work, see below. As the definition of the index - value pairs are supposed to become part of the API, does anyone have an idea how...
0
9000
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9339
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,...
0
9256
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 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...
0
8260
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6804
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6081
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();...
0
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3322
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
3
2225
bsmnconsultancy
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...

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.