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

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 1728
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*****@mindspring.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.learn.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*****@mindspring.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*****@mindspring.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
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...
0
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...
4
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...
13
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...
8
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
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) ...
4
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>...
3
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
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...
3
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...
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: 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: 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
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
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,...

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.