Guys,
I was just looking at some code where to initialize an integer
with all F's following statement is used;
unsigned int i = -1;
I want to know if this is the right way of doing the things ? if not,
what is the correct way to do this ? 38 2200 ju**********@ya hoo.co.in wrote:
Guys,
I was just looking at some code where to initialize an integer
with all F's following statement is used;
unsigned int i = -1;
I want to know if this is the right way of doing the things ? if not,
what is the correct way to do this ?
unsigned int i = ~0U;
initializes to all 1's. There is no way to initialize to all
(presumably, hex) F's on a hypothetical 41-bit machine.
<ju**********@y ahoo.co.inwrote in message
news:11******** **************@ q4g2000prc.goog legroups.com...
Guys,
I was just looking at some code where to initialize an integer
with all F's following statement is used;
unsigned int i = -1;
I want to know if this is the right way of doing the things ? if not,
what is the correct way to do this ?
It is one of those awkward things.
In practise you are never likely to program a non-two's complement machine.
However one's complement and sign magnitude are allowed (one place you will
see non-two's complement integers is in the exponent of an IEEE floating
point number).
So i = ~0; is actually the best way of achieving things. But you will see i
= -1 even in production code.
--
Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm
"ju**********@y ahoo.co.in" wrote:
>
I was just looking at some code where to initialize an integer
with all F's following statement is used;
unsigned int i = -1;
I want to know if this is the right way of doing the things ?
if not, what is the correct way to do this ?
It's not an integer, it's an unsigned int. Not the same. And yes,
the unsigned int will be initialized to 2**n - 1. If "sizeof
unsigned int * CHAR_BIT" is a multiple of 8, that will be a
collection of hex f.
Copying that unsigned int into an int will normally cause undefined
behaviour, because it out of range for an int. That behaviour may
be exactly what you want, or anything else. You just don't know.
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
--
Posted via a free Usenet account from http://www.teranews.com
On Aug 29, 11:10 am, "junky_fel...@y ahoo.co.in"
<junky_fel...@y ahoo.co.inwrote :
Guys,
I was just looking at some code where to initialize an integer
with all F's following statement is used;
unsigned int i = -1;
I want to know if this is the right way of doing the things ? if not,
what is the correct way to do this ?
Another way is :-
int i = 0xFFFF;
ravi wrote:
On Aug 29, 11:10 am, "junky_fel...@y ahoo.co.in"
<junky_fel...@y ahoo.co.inwrote :
>Guys,
I was just looking at some code where to initialize an integer with all F's following statement is used;
unsigned int i = -1;
I want to know if this is the right way of doing the things ? if not, what is the correct way to do this ?
Another way is :-
int i = 0xFFFF;
Assuming 4-bytes int.
--
Pietro Cerutti
PGP Public Key: http://gahr.ch/pgp
Pietro Cerutti wrote:
ravi wrote:
>On Aug 29, 11:10 am, "junky_fel...@y ahoo.co.in" <junky_fel...@ yahoo.co.inwrot e:
>>Guys,
I was just looking at some code where to initialize an integer with all F's following statement is used;
unsigned int i = -1;
I want to know if this is the right way of doing the things ? if not, what is the correct way to do this ?
Another way is :-
int i = 0xFFFF;
Assuming 4-bytes int.
hem... assuming 2-bytes ints.
Check out this:
#include <stdio.h>
int main(void) {
unsigned int i = 0xFFFFU;
unsigned int ii = ~0U;
printf("i is %u, ll is %u\n", i, ii);
return (0);
}
--
Pietro Cerutti
PGP Public Key: http://gahr.ch/pgp
Malcolm McLean wrote:
>
<ju**********@y ahoo.co.inwrote in message
news:11******** **************@ q4g2000prc.goog legroups.com...
Guys,
I was just looking at some code where to initialize an integer
with all F's following statement is used;
unsigned int i = -1;
So i = ~0; is actually the best way of achieving things.
That's wrong.
unsigned int i = -1;
is guaranteed to put a value of UINT_MAX into object i.
i = ~0; operates on the sign bit of an int type value
and yields an implementation defined result.
N869
6.2.5 Types
A computation involving unsigned operands
can never overflow, because a result that cannot be
represented by the resulting unsigned integer type is
reduced modulo the number that is one greater than the
largest value that can be represented by the resulting type.
--
pete
Ark Khasin wrote:
> ju**********@ya hoo.co.in wrote:
Guys,
I was just looking at some code where to initialize an integer
with all F's following statement is used;
unsigned int i = -1;
I want to know if this is the right way of doing the things ?
if not, what is the correct way to do this ?
unsigned int i = ~0U;
initializes to all 1's.
So does unsigned int i = -1;
--
pete ju**********@ya hoo.co.in wrote:
>
Guys,
I was just looking at some code where to initialize an integer
with all F's following statement is used;
unsigned int i = -1;
I want to know if this is the right way of doing the things ?
That is one correct way to initialise an unsigned int
object with a value of UINT_MAX
--
pete This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dan Perl |
last post by:
There is something with initializing mutable class attributes that I am
struggling with. I'll use an example to explain:
class Father:
attr1=None # this is OK
attr2= # this is wrong
def foo(self, data):
self.attr1=data
self.attr2.append(data)
The initialization of attr1 is obviously OK, all instances of Father
redefine it in the method foo. But the initialization of attr2 is wrong
|
by: Avi Uziel |
last post by:
Hi All,
I'm writing a Windows DLL which contain some utility classes.
One of my classes is Singleton, therefore contain some static members.
I'm using VC6 and linking to the DLL statically.
My question is where is the correct (and best way) to initialize
theses variables? If I initialize them in the class CPP (in the DLL)
then I get linkage error when I try to link my project with the LIB,
and I don't want to make the user initialize...
|
by: hrmadhu |
last post by:
Hi,
I wish to declare a vector of deque of int, which I do as follows.
#include<vector>
#include<deque>
#include<iostream>
using namespace std;
int main(int argc, char* argv)
{
|
by: newsposter0123 |
last post by:
The code block below initialized a r/w variable (usually .bss) to the
value of pi. One, of many, problem is any linked compilation unit may
change the global variable. Adjusting
// rodata
const long double const_pi=0.0;
lines to
// rodata
|
by: alacrite |
last post by:
If I have this situation
class X
{
Z z;
Y y;
};
Class X has two objects of type Z and Y. How do I initialize z and y
with non default constructors?
| |
by: SM |
last post by:
I've always wonder if there is diference when declaring and
initializing a varible inside/outside a loop.
What's a better practice?
Declaring and initializing variables inside a loop routine, like this:
for(var i=0; i<list; i++)
{
var name = list;
}
|
by: Jason Doucette |
last post by:
Situation:
I have a simple struct that, say, holds a color (R, G, and B). I
created my own constructors to ease its creation. As a result, I lose
the default constructor. I dislike this, but it's easy to solve: I
just make my own default constructor.
Problem:
My own default constructor is considered to be *initializing the
variable* (even though it doesn't), whereas the original one does
not. Thus, when I declare and use it before...
|
by: Ray |
last post by:
Hi all,
After many years of C, I thought I'd move to C++ recently. I think
because I think in C, I'm coming to a misunderstanding of something.
I've created a class foo which contains a private variable which is a
priority queue. In class foo's header file, I declared it as:
class foo {
private:
|
by: WaterWalk |
last post by:
Hello. When I consult the ISO C++ standard, I notice that in
paragraph 3.6.2.1, the standard states:
"Objects with static storage duration shall be zero-initialized before
any other initialization takes place."
Does this mean all non-local objects will be zero-initialized before
they are initialized by their initializers(if they have)? For example:
int g_var = 3;
int main() {}
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |