473,804 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_netCol or = 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 "transparen cy" 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 6187
Ce**********@gm ail.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**********@g mail.comwrote in message
news:11******** *************@i 39g2000hsf.goog legroups.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_netCol or = 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 "transparen cy" 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**********@g mail.comwrote in message
news:11******** *************@i 39g2000hsf.goog legroups.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_netCol or = 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
437
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
3955
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
9532
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 use "long long" integer type(64 bit), but it seems that Oracle doesn't like it after execution, and giving me error code -1460, any idea about this? thx
21
2829
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 convenient (and for some things possibly necessary) to use long long integer and/or unsigned long long integer variables. How widely supported are these variable types? How long ago were they introduced? I notice they are not mentioned in K&R #2.
12
13533
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 sprintf(pHex,"%0X",9987967441778573855). But it only returns 8
2
7281
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 single line with "\n" in the middle, or I could use multiple cout and endl. But I just feel more readable if I can have the whole paragraph as a string. Thanks,
21
2655
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), sometimes int and long int are the same, and long long int is twice the width; or sometimes both long int and long long int are twice the width of int. This apparently all quite normal according to my c99 draft and c-faq.com. However it doesn't...
10
3840
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
2766
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++){ long long int k = 1<<i; cout<<i<<"\t"<<k<<"\t"<<sizeof(k)<<"\n"; }
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10603
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
10353
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...
1
10356
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
9176
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
7643
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
6869
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
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.