473,700 Members | 2,754 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 1750
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
5230
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
1418
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
3880
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
1549
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
4719
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
1776
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
10340
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
2108
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
1895
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
2392
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
8712
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...
0
9203
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9058
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8911
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
7794
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...
0
5895
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
4649
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2375
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2018
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.