toggle bit in unsigned long var

This is a discussion on toggle bit in unsigned long var within the C++ Programming forums, part of the General Programming Boards category; Hello, I want to toggle bits in an unsigned long variable. But I get an overflow with bits higher then ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    115

    toggle bit in unsigned long var

    Hello,

    I want to toggle bits in an unsigned long variable. But I get an overflow with bits higher then 31.
    Any ideas how to solve this? Thank you.

    Code:
    unsigned long n = 63;
    unsigned long c = 0x8000000000000000;
    c ^= (1 << n);
    
    Output: 
    
    01111111 | 11111111 | 11111111 | 11111111 | 10000000 | 00000000 | 00000000 | 00000000

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    Of course you do. Typically, a long is 32 bits long.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    If I change my code to the following:

    Code:
    unsigned long long n = 63;
    unsigned long long c = 0x8000000000000000;
    c ^= (1 << n);
    shouldn't it work then? I still have the same problem.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by thescratchy View Post
    If I change my code to the following:

    Code:
    unsigned long long n = 63;
    unsigned long long c = 0x8000000000000000;
    c ^= (1 << n);
    shouldn't it work then? I still have the same problem.
    The value "1" is 32 bits. You shift that left 63 and you get zero. Write "1LL" instead.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    This works as expected:
    Code:
    int main()
    {
    	unsigned long long n = 63;
    	unsigned long long c = 0x8000000000000000;
    	unsigned long long mask = 1ull << n;
    	c ^= mask;
    }
    Also, IIRC, shifting a signed number is implementation defined.
    EDIT: More exactly, shifting a negative signed number is either implementation defined or undefined, so avoid it.
    Last edited by Elysia; 02-19-2011 at 09:15 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    Thank you guys for your help

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    31
    Quote Originally Posted by thescratchy View Post
    If I change my code to the following:

    Code:
    unsigned long long n = 63;
    unsigned long long c = 0x8000000000000000;
    c ^= (1 << n);
    shouldn't it work then? I still have the same problem.

    Try:
    Code:
        c ^= (1ull << n);

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by kona49er View Post
    Try:
    Code:
        c ^= (1ull << n);
    Better yet:

    Code:
    #include <limits>
    template < typename Integer >
    Integer msb( void ) {
        static Integer msk = Integer( 1 ) << sizeof( Integer ) 
            * std::numeric_limits< unsigned char >::digits - 1;
        return msk;
    }
    
    // Example:
    
    #include <iostream>
    int main( void ) {
        using namespace std;
        cout << int( msb< unsigned char >( ) ) << endl;
        cout << msb< short >( ) << endl;
        cout << msb< unsigned long >( ) << endl;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  2. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 05:20 PM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 08:22 AM
  4. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 01:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21