public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* c++, gcc3.2.2 and bitwise operations
@ 2003-02-19  1:36 Chris Croswhite
  2003-02-19 11:56 ` Sebastian Huber
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Chris Croswhite @ 2003-02-19  1:36 UTC (permalink / raw)
  To: gcc-help

How can I play with the guts of a variable one bit at a time in c++?  That
is, I want to do something like this,

long long val =0x4000000000000001;
((int*)(&val)[1]) &= 0x0;

(this should set the first bit to zero).  This worked with gcc 2.95.x, but I
understand now that the casting (&val)[1] (to an array) is "undefined" and
therefore not predictible.

Could anyone give me hand?

TIA,
Chris


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: c++, gcc3.2.2 and bitwise operations
  2003-02-19  1:36 c++, gcc3.2.2 and bitwise operations Chris Croswhite
@ 2003-02-19 11:56 ` Sebastian Huber
  2003-02-19 12:25 ` Nathan Sidwell
  2003-02-19 13:08 ` John Love-Jensen
  2 siblings, 0 replies; 4+ messages in thread
From: Sebastian Huber @ 2003-02-19 11:56 UTC (permalink / raw)
  To: gcc-help

On Wednesday 19 February 2003 02:33, Chris Croswhite wrote:
> How can I play with the guts of a variable one bit at a time in c++?  That
> is, I want to do something like this,
>
> long long val =0x4000000000000001;
> ((int*)(&val)[1]) &= 0x0;
>
> (this should set the first bit to zero).
> [...]

Hi,
the standard way to to this is:
val &= ~(0x1LL)

Bye


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: c++, gcc3.2.2 and bitwise operations
  2003-02-19  1:36 c++, gcc3.2.2 and bitwise operations Chris Croswhite
  2003-02-19 11:56 ` Sebastian Huber
@ 2003-02-19 12:25 ` Nathan Sidwell
  2003-02-19 13:08 ` John Love-Jensen
  2 siblings, 0 replies; 4+ messages in thread
From: Nathan Sidwell @ 2003-02-19 12:25 UTC (permalink / raw)
  To: ccroswhite; +Cc: gcc-help

Chris Croswhite wrote:
> How can I play with the guts of a variable one bit at a time in c++?  That
> is, I want to do something like this,
> 
> long long val =0x4000000000000001;
> ((int*)(&val)[1]) &= 0x0;
> 
> (this should set the first bit to zero).  This worked with gcc 2.95.x, but I
> understand now that the casting (&val)[1] (to an array) is "undefined" and
> therefore not predictible.
> 
> Could anyone give me hand?
a) read the documentation about type based aliasing
b) don't do that :)

nathan

-- 
Nathan Sidwell    ::   http://www.codesourcery.com   ::     CodeSourcery LLC
          The voices in my head said this was stupid too
nathan@codesourcery.com : http://www.cs.bris.ac.uk/~nathan/ : nathan@acm.org


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: c++, gcc3.2.2 and bitwise operations
  2003-02-19  1:36 c++, gcc3.2.2 and bitwise operations Chris Croswhite
  2003-02-19 11:56 ` Sebastian Huber
  2003-02-19 12:25 ` Nathan Sidwell
@ 2003-02-19 13:08 ` John Love-Jensen
  2 siblings, 0 replies; 4+ messages in thread
From: John Love-Jensen @ 2003-02-19 13:08 UTC (permalink / raw)
  To: ccroswhite, gcc-help

Hi Chris,

> How can I play with the guts of a variable one bit at a time in c++?

The C++ way is to use the #include <bitset>, as describe in Stroustrup's
C++PL (3rd) in section 17.5.3.

Sincerely,
--Eljay

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2003-02-19 13:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-19  1:36 c++, gcc3.2.2 and bitwise operations Chris Croswhite
2003-02-19 11:56 ` Sebastian Huber
2003-02-19 12:25 ` Nathan Sidwell
2003-02-19 13:08 ` John Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).