What's the best portable way to set the MSB of an unsigned integer type?
Is the following any good?:
Start of with: 0000 0000
Flip all the bits: 1111 1111
Shift once to the right: 0111 1111
Flip all the bits: 1000 0000
Here it is done in code:
typedef unsigned short UIntType;
UIntType msb_only =
~( ~( (UIntType)0 ) >> 1 );
Is there a better way?
-Tomás 11 12340
"Tomás" <No.Email@Address> wrote: What's the best portable way to set the MSB of an unsigned integer type?
Is the following any good?:
Start of with: 0000 0000 Flip all the bits: 1111 1111 Shift once to the right: 0111 1111 Flip all the bits: 1000 0000
Here it is done in code:
typedef unsigned short UIntType;
UIntType msb_only = ~( ~( (UIntType)0 ) >> 1 );
Is there a better way?
Because of the way unsigned integer overflow is handled in C, you can
replace the first two steps with converting -1 to the desired type. This
results in
UIntType msb_only = ~( (UIntType)-1 >> 1 );
This is obviously shorter; up to you to decide whether you find it as
legible.
Richard
On 2006-06-02, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote: "Tomás" <No.Email@Address> wrote:
What's the best portable way to set the MSB of an unsigned integer type?
Is the following any good?:
Start of with: 0000 0000 Flip all the bits: 1111 1111 Shift once to the right: 0111 1111 Flip all the bits: 1000 0000
Here it is done in code:
typedef unsigned short UIntType;
UIntType msb_only = ~( ~( (UIntType)0 ) >> 1 );
Is there a better way?
Because of the way unsigned integer overflow is handled in C, you can replace the first two steps with converting -1 to the desired type. This results in
UIntType msb_only = ~( (UIntType)-1 >> 1 );
This is obviously shorter; up to you to decide whether you find it as legible.
I would take one and left-shift it sizeof(type) * CHAR_BIT.
This solution is pretty easy to read:
int m = 1 << (sizeof m * CHAR_BIT) /* Set MSB */
--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
You can lead a blind man to water but you can't make him chug it.
Andrew Poelstra wrote On 06/02/06 11:26,: On 2006-06-02, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote: "Tomás" <No.Email@Address> wrote:
What's the best portable way to set the MSB of an unsigned integer type?
Is the following any good?:
Start of with: 0000 0000 Flip all the bits: 1111 1111 Shift once to the right: 0111 1111 Flip all the bits: 1000 0000
Here it is done in code:
typedef unsigned short UIntType;
UIntType msb_only = ~( ~( (UIntType)0 ) >> 1 );
Is there a better way?
Because of the way unsigned integer overflow is handled in C, you can replace the first two steps with converting -1 to the desired type. This results in
UIntType msb_only = ~( (UIntType)-1 >> 1 );
This is obviously shorter; up to you to decide whether you find it as legible. I would take one and left-shift it sizeof(type) * CHAR_BIT. This solution is pretty easy to read: int m = 1 << (sizeof m * CHAR_BIT) /* Set MSB */
This is wrong. R-O-N-G, wrong. Where to begin?
- It assumes all bits of an `int' are value bits, and
ignores the possibility of padding bits. All right,
that may be more of a "theoretical" than an "actual"
problem, but it's not the only one ...
- Shift operators are only defined when the shift
distance is strictly less than the width of the
shifted value. There's a `-1' missing, without which
the above yields undefined behavior (6.5.7/3). On
actual machines, the likely result is `m=0' or `m=1'.
- Even with the `-1', the above is an attempt to shift
a value bit into the sign position, which once again
yields undefined behavior (6.5.7/4). The missing `-1'
should perhaps be a `-2', depending on how you choose
to define the "M"SB of a signed integer.
- Speaking of signed integers, the O.P. specifically
asked about *un*signed integers.
If somebody offers you this "solution," I'd recommend
that you not drink it.
-- Er*********@sun.com
"Tomás" <No.Email@Address> wrote:
#
# What's the best portable way to set the MSB of an unsigned integer type?
Is MSB a meaningful concept in portable code?
--
SM Ryan http://www.rawbw.com/~wyrmwif/
Don't say anything. Especially you.
SM Ryan wrote On 06/02/06 13:05,: "Tomás" <No.Email@Address> wrote: # # What's the best portable way to set the MSB of an unsigned integer type? Is MSB a meaningful concept in portable code?
It's portable in the same sense that UINT_MAX is
portable. Every implementation has a UINT_MAX, even
though the value of UINT_MAX is implementation-dependent.
Every unsigned integer type has a Most Significant Bit,
even though its value is implementation-dependent.
-- Er*********@sun.com
On 2006-06-02, Eric Sosman <Er*********@sun.com> wrote:
Andrew Poelstra wrote On 06/02/06 11:26,: On 2006-06-02, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
"Tomás" <No.Email@Address> wrote:
What's the best portable way to set the MSB of an unsigned integer type?
Is the following any good?:
Start of with: 0000 0000 Flip all the bits: 1111 1111 Shift once to the right: 0111 1111 Flip all the bits: 1000 0000
Here it is done in code:
typedef unsigned short UIntType;
UIntType msb_only = ~( ~( (UIntType)0 ) >> 1 );
Is there a better way?
Because of the way unsigned integer overflow is handled in C, you can replace the first two steps with converting -1 to the desired type. This results in
UIntType msb_only = ~( (UIntType)-1 >> 1 );
This is obviously shorter; up to you to decide whether you find it as legible.
I would take one and left-shift it sizeof(type) * CHAR_BIT.
This solution is pretty easy to read:
int m = 1 << (sizeof m * CHAR_BIT) /* Set MSB */
This is wrong. R-O-N-G, wrong. Where to begin?
- It assumes all bits of an `int' are value bits, and ignores the possibility of padding bits. All right, that may be more of a "theoretical" than an "actual" problem, but it's not the only one ...
- Shift operators are only defined when the shift distance is strictly less than the width of the shifted value. There's a `-1' missing, without which the above yields undefined behavior (6.5.7/3). On actual machines, the likely result is `m=0' or `m=1'.
- Even with the `-1', the above is an attempt to shift a value bit into the sign position, which once again yields undefined behavior (6.5.7/4). The missing `-1' should perhaps be a `-2', depending on how you choose to define the "M"SB of a signed integer.
- Speaking of signed integers, the O.P. specifically asked about *un*signed integers.
If somebody offers you this "solution," I'd recommend that you not drink it.
Point 1 is generally not a concern for primitive types.
Point 2 is correct; I did forget a -1.
Point 3 is eliminated by point 4, and in fact.
My definition of MSB is leftmost bit. My solution (with a -1)
works by that definition.
--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
You can lead a blind man to water but you can't make him chug it.
Andrew Poelstra wrote On 06/02/06 14:59,: On 2006-06-02, Eric Sosman <Er*********@sun.com> wrote: Andrew Poelstra wrote On 06/02/06 11:26,:
On 2006-06-02, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
"Tomás" <No.Email@Address> wrote: >What's the best portable way to set the MSB of an unsigned integer type? > >Is the following any good?: > > Start of with: 0000 0000 > Flip all the bits: 1111 1111 > Shift once to the right: 0111 1111 > Flip all the bits: 1000 0000 > >Here it is done in code: > > typedef unsigned short UIntType; > > UIntType msb_only = > ~( ~( (UIntType)0 ) >> 1 ); > >Is there a better way?
Because of the way unsigned integer overflow is handled in C, you can replace the first two steps with converting -1 to the desired type. This results in
UIntType msb_only = ~( (UIntType)-1 >> 1 );
This is obviously shorter; up to you to decide whether you find it as legible.
I would take one and left-shift it sizeof(type) * CHAR_BIT.
This solution is pretty easy to read:
int m = 1 << (sizeof m * CHAR_BIT) /* Set MSB */
This is wrong. R-O-N-G, wrong. Where to begin?
- It assumes all bits of an `int' are value bits, and ignores the possibility of padding bits. All right, that may be more of a "theoretical" than an "actual" problem, but it's not the only one ...
- Shift operators are only defined when the shift distance is strictly less than the width of the shifted value. There's a `-1' missing, without which the above yields undefined behavior (6.5.7/3). On actual machines, the likely result is `m=0' or `m=1'.
- Even with the `-1', the above is an attempt to shift a value bit into the sign position, which once again yields undefined behavior (6.5.7/4). The missing `-1' should perhaps be a `-2', depending on how you choose to define the "M"SB of a signed integer.
- Speaking of signed integers, the O.P. specifically asked about *un*signed integers.
If somebody offers you this "solution," I'd recommend that you not drink it.
Point 1 is generally not a concern for primitive types. Point 2 is correct; I did forget a -1. Point 3 is eliminated by point 4, and in fact. My definition of MSB is leftmost bit. My solution (with a -1) works by that definition.
Well, it works once you've changed from `int' to
`unsigned int' (in *two* places) and tacked on a `-1',
provided there are no padding bits. Putting all this
together and generalizing to types that might be wider
than an `int', you wind up with
UIntType m = (UIntType)1 << (CHAR_BIT * sizeof m - 1);
Readability is in the eye of the beholder, but I don't
find this more readable than
UIntType m = ((UIntType)-1 >> 1) + 1;
... which has the virtues of being both bullet-proof and
shorter. This beholder's eye sees no reason to prefer
the longer, shakier construct.
By the way, note that `~((UIntType)-1 >> 1)' is not
guaranteed to work. If UIntType is sufficiently narrow it
will be subject to the "integer promotions" and the value
inside the parentheses will be a non-negative signed `int'
(non-negative because otherwise promotion wouldn't have
occurred). Applying `~' yields a non-positive value, but
just what that value is depends on how the system represents
negative integers. On a ones' complement machine, converting
back to UIntType would give an unintended result.
-- Er*********@sun.com
Tomás wrote: What's the best portable way to set the MSB of an unsigned integer type?
Is the following any good?:
Start of with: 0000 0000
Flip all the bits: 1111 1111
Shift once to the right: 0111 1111
Flip all the bits: 1000 0000
Here it is done in code:
typedef unsigned short UIntType;
UIntType msb_only = ~( ~( (UIntType)0 ) >> 1 );
Is there a better way?
-Tomás
unsigned integer type, hmm, you probably mean a 32 bit unsigned int although
your example uses an 8 bit type.
Anyway for 32 bits v = v | 0x80000000;
for 8 bits (like your example)
v = v|0x80;
Eric
Eric schrieb: Tomás wrote:
What's the best portable way to set the MSB of an unsigned integer type?
Is the following any good?:
Start of with: 0000 0000
Flip all the bits: 1111 1111
Shift once to the right: 0111 1111
Flip all the bits: 1000 0000
Here it is done in code:
typedef unsigned short UIntType;
UIntType msb_only = ~( ~( (UIntType)0 ) >> 1 );
Is there a better way? unsigned integer type, hmm, you probably mean a 32 bit unsigned int although your example uses an 8 bit type.
The OP clearly stated that he is looking for a general solution for
unsigned integer types. The thing is that you do _not_ know the number
of value bits of the type beforehand; assuming padding bits, it may be
not equal to the type's width.
Anyway for 32 bits v = v | 0x80000000; for 8 bits (like your example) v = v|0x80;
What is that the solution for?
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Tomás wrote: What's the best portable way to set the MSB of an unsigned integer type?
Define best.
Is the following any good?:
Start of with: 0000 0000 Flip all the bits: 1111 1111 Shift once to the right: 0111 1111 Flip all the bits: 1000 0000
Here it is done in code:
typedef unsigned short UIntType;
UIntType msb_only = ~( ~( (UIntType)0 ) >> 1 );
This can fail if UIntType has a rank lower than int. Try...
UIntType msb_only = ((uintN_t) -1)/2+1;
If UIntType is unsigned or unsigned long, then the notation is
even cleaner, e.g. ...
unsigned msb = -1u/2+1;
--
Peter
Eric Sosman wrote: UIntType m = ((UIntType)-1 >> 1) + 1;
That's what I use to set the MSB.
It's both simple and bulletproof, as you have already stated.
--
pete This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: John J |
last post by:
I've written the following code into a class to search for and display the
results of all races entered (The complete code is in a previous thread). I
wish to amend the code so as to display the...
|
by: Matt Kruse |
last post by:
http://www.JavascriptToolbox.com/bestpractices/
I started writing this up as a guide for some people who were looking for
general tips on how to do things the 'right way' with Javascript. Their...
|
by: hagai26 |
last post by:
I am looking for the best and efficient way to replace the first word
in a str, like this:
"aa to become" -> "/aa/ to become"
I know I can use spilt and than join them
but I can also use regular...
|
by: Irene |
last post by:
Hi all,
I have set up a simple VB program (and later on an ASP interface) to
manage an Athletics database. I'm using Access 2000.
To simplify, I have the Athlets, the Competitions and the...
|
by: l.woods |
last post by:
I want your recommendation on which ASP.NET Shopping Cart software I should
buy?
Best code
Best documentation
Best support (if needed. I will buying source code, if possible)
TIA,
Larry...
|
by: jojobar |
last post by:
Hello,
I am trying to use vs.net 2005 to migrate a project originally in vs.net
2003. I started with creation of a "web site", and then created folders for
each component of the site.
I read...
|
by: Ren |
last post by:
Hi All,
I'm still rather new at vb.net and would like to know the proper way to access
private varibables in a class. Do I access the variable directly or do I use
the public property?
...
|
by: Ned Balzer |
last post by:
Hi all,
I am pretty new to asp.net; I've done lots of classic asp, but am just
beginning to get my mind wrapped around .net.
What I'd like to do is include some code that tests if a user is...
|
by: Earl |
last post by:
I have all of my data operations in a separate library, so I'm looking for
what might be termed "best practices" on a return type from those classes.
For example, let's say I send an update from...
|
by: ankitmathur |
last post by:
Hi,
I'm trying to overcome a situation whereby I have to search through 4-5 columns and produce the results with an order by according to the values matched in these columns.
Example:
My...
|
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,...
|
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$) {
}
...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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: 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...
| |