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

Why is 0xff000000 a long?

Greetings!

I am trying to write a color class that is a bit more intelligent that
C#'s System.Color structure. This means that I'm using a bunch of
bitmasks. My class has the following method:

public static HTColor FromArgb(int argb)
{
int colorValue = 0;
int transparency = (argb & 0xff000000) >24;
int red = (argb & 0xff0000) >16;
int green = (argb & 0x00ff00) >8;
int blue = argb & 0x0000ff;
HTColor result;
result.m_netColor = Color.FromArgb(red, green, blue);
return result;
}

The transparency line is throwing an error claiming that I cannot
implicity convert an object of type 'long' to type 'int'.

But the C# standard for literals includes the following:

"1 The type of an integer literal is determined as follows:
2 If the literal has no suffix, it has the first of these types in
which its value can be represented: int, uint, long, ulong.
3 If the literal is suffixed by U or u, it has the first of these types
in which its value can be represented: uint, ulong.
4 If the literal is suffixed by L or l, it has the first of these types
in which its value can be represented: long, ulong.
5 If the literal is suffixed by UL, Ul, uL, ul, LU, Lu, lU, or lu, it
is of type ulong."
(quoted from
http://www.jaggersoft.com/csharp_sta...er-type-suffix)

Since ints and uints are at least 32 bits long and 0xff000000 is 32
bits long, it seems to me that this literal should be evaluated as an
int.

I get the same error if my literal is 0x8f000000, and I don't get the
error if my literal is 0x7f000000.

Thank you very much.

Rob Richardson
RAD-CON, Inc.

P.S. Yes, I know "transparency" is never used. I'm not sure if I even
want it, but I do want to understand the literal type issue it
illustrates.

Jan 10 '07 #1
4 6144
Ce**********@gmail.com wrote:

int transparency = (argb & 0xff000000) >24;
The transparency line is throwing an error claiming that I cannot
implicity convert an object of type 'long' to type 'int'.
Mixing 'int' and 'uint' results in 'long' to avoid treating a signed
type as an unsigned type, or vice versa (as this would result in
changing the semantic value stored in the variable).
But the C# standard for literals includes the following:

"1 The type of an integer literal is determined as follows:
2 If the literal has no suffix, it has the first of these types in
which its value can be represented: int, uint, long, ulong.
3 If the literal is suffixed by U or u, it has the first of these types
in which its value can be represented: uint, ulong.
4 If the literal is suffixed by L or l, it has the first of these types
in which its value can be represented: long, ulong.
5 If the literal is suffixed by UL, Ul, uL, ul, LU, Lu, lU, or lu, it
is of type ulong."
(quoted from
http://www.jaggersoft.com/csharp_sta...er-type-suffix)

Since ints and uints are at least 32 bits long and 0xff000000 is 32
bits long, it seems to me that this literal should be evaluated as an
int.
0xff000000 is not negative (it has no leading -), therefore it cannot be
an int (the highest int is 0x7fffffff). However, it can be a uint - and
that is in fact its type. Try this:

uint x = 0xff000000;

You'll get no error.
I get the same error if my literal is 0x8f000000, and I don't get the
error if my literal is 0x7f000000.
That's because 0x8f000000 is larger than the largest positive int, while
0x7f000000 is smaller than the largest positive int.

Beware mixing signed and unsigned.

-- Barry

--
http://barrkel.blogspot.com/
Jan 10 '07 #2
The problem is getting it into transparency, no converting the 0xff.. to
long...
Try
long transparency = (argb & 0xff000000) >24;

and I would have a tendency to do this so everything is the same type:
long transparency = ((long)argb & 0xff000000) >24;

Regards,
John

<Ce**********@gmail.comwrote in message
news:11*********************@i39g2000hsf.googlegro ups.com...
Greetings!

I am trying to write a color class that is a bit more intelligent that
C#'s System.Color structure. This means that I'm using a bunch of
bitmasks. My class has the following method:

public static HTColor FromArgb(int argb)
{
int colorValue = 0;
int transparency = (argb & 0xff000000) >24;
int red = (argb & 0xff0000) >16;
int green = (argb & 0x00ff00) >8;
int blue = argb & 0x0000ff;
HTColor result;
result.m_netColor = Color.FromArgb(red, green, blue);
return result;
}

The transparency line is throwing an error claiming that I cannot
implicity convert an object of type 'long' to type 'int'.

But the C# standard for literals includes the following:

"1 The type of an integer literal is determined as follows:
2 If the literal has no suffix, it has the first of these types in
which its value can be represented: int, uint, long, ulong.
3 If the literal is suffixed by U or u, it has the first of these types
in which its value can be represented: uint, ulong.
4 If the literal is suffixed by L or l, it has the first of these types
in which its value can be represented: long, ulong.
5 If the literal is suffixed by UL, Ul, uL, ul, LU, Lu, lU, or lu, it
is of type ulong."
(quoted from
http://www.jaggersoft.com/csharp_sta...er-type-suffix)

Since ints and uints are at least 32 bits long and 0xff000000 is 32
bits long, it seems to me that this literal should be evaluated as an
int.

I get the same error if my literal is 0x8f000000, and I don't get the
error if my literal is 0x7f000000.

Thank you very much.

Rob Richardson
RAD-CON, Inc.

P.S. Yes, I know "transparency" is never used. I'm not sure if I even
want it, but I do want to understand the literal type issue it
illustrates.

Jan 10 '07 #3
Thanks to both of you. I understand.

Rob

Jan 10 '07 #4

<Ce**********@gmail.comwrote in message
news:11*********************@i39g2000hsf.googlegro ups.com...
Greetings!

I am trying to write a color class that is a bit more intelligent that
C#'s System.Color structure. This means that I'm using a bunch of
bitmasks. My class has the following method:

public static HTColor FromArgb(int argb)
{
int colorValue = 0;
int transparency = (argb & 0xff000000) >24;
int red = (argb & 0xff0000) >16;
int green = (argb & 0x00ff00) >8;
int blue = argb & 0x0000ff;
HTColor result;
result.m_netColor = Color.FromArgb(red, green, blue);
return result;
}
Shift first, then mask, to avoid evil sign-extension.
Jan 10 '07 #5

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

Similar topics

10
by: RA Scheltema | last post by:
hi all, A small question about serializing and deserializing a long in a platform independent manner. Can this be done with the following code ?: char buf; long val = 35456;
9
by: luke | last post by:
Hi everybody, please, can someone explain me this behaviour. I have the following piece of code: long long ll; unsigned int i = 2; ll = -1 * i; printf("%lld\n", ll);
12
by: wenmang | last post by:
Hi, I am using following Oracle Proc-C compiler: Pro*C/C++: Release 8.1.7.0.0 - Production on Thu Jun 15 15:57:32 2006 (c) Copyright 2000 Oracle Corporation. All rights reserved. I like to...
21
by: Charles Sullivan | last post by:
I maintain/enhance some inherited FOSS software in C which has compiler options for quite a few different Unix-like operating systems, many of which I've never even heard of. It would be...
12
by: Ahmad Jalil Qarshi | last post by:
Hi, I have an integer value which is very long like 9987967441778573855. Now I want to convert it into equivalent Hex value. The result must be 8A9C63784361021F I have used...
2
by: PengYu.UT | last post by:
Hi, In python, triple quote (""") can be used to quote a paragraph (multiple lines). I'm wondering if there is any equivalent in C++. For the following code, I could write the long string in a...
21
by: Bart C | last post by:
I've always had a problem knowing exactly how wide my integer variables were in C, and the little program below has increased my confusion. Run on 3 compilers on the same cpu (32-bit pentium),...
10
by: ratcharit | last post by:
Currently using cosine function in math.h Currently I get: 1 = cos(1e^-7) Is there another way for cos to return value of high accuracy say: 0.999999 = cos(1e^-7)
15
by: Oliver Graeser | last post by:
I need a >49 bit integer type. tried sizeof(long long), says 8. 8 byte = 64 bit right? but when I try to assign a value with more than 32 bit, it fails. To illustrate: for (i=0; i<64; i++){...
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: 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
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: 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
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,...
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
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...

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.