Thread: Bit Setting for long long

  1. #1
    Registered User
    Join Date
    Oct 2009

    Bit Setting for long long

    I'm trying to write an expression in a single line that can do the following:

    take the sizeof(type), then fill a long long variable with 1's for the amount of bytes given by the sizeof.

    For example:

    type = long - then my number is: 0x00000000ffffffff

    type = long long - then my number is 0xffffffffffffffff


  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    How will you handle this type X?
    struct X
        long long a, b;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2009
    i need to do this:

    static const double SCALE = 2.0 / VALUE;

    where value is the variable i need to calculate with the sizeof. So I can fill half of VALUE with type long, and all of VALUE with type long long.

  4. #4
    i've lost my mind
    Join Date
    Jun 2008
    edit: iMalc gave a way better example below, I removed my /fail pseudo code.

    The C++ witch makes a good point, not only could you receive a struct larger than any of the basic/standard data types, but you could receive a struct with two or three chars, you won't actually know what you're dealing with unless you require some extra input.

    I don't know what you're trying to do, but it may not work out like you want without convoluted functions (eww gross!) in this approach. ;p
    Last edited by gltiich; 10-13-2009 at 01:53 PM.

  5. #5
    The larch
    Join Date
    May 2006
    May-be a template function:

    #include <iostream>
    template <class N>
    N all_ones()
        return ~N();
    int main()
        unsigned long long a = all_ones<unsigned long>();
        unsigned long long b = all_ones<unsigned long long>();
        std::cout << std::hex << a << '\n' << b << '\n';
    This won't work with signed types, though, because the result would be seen as -1 in either case, and the implicit cast from -1L to -1LL would flip the rest of the bits.

    However, it also appears to me that you might just want to divide 2.0 by the maximum value of corresponding unsigned type:

    #include <limits>
    double d = 2.0 / std::numeric_limits<unsigned long>::max();
    Be warned that you could lose some precision with unsigned long long, since its maximum value might not be exactly representable as double.
    Last edited by anon; 10-13-2009 at 08:59 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    ~0ULL>>(64-sizeof(type)*8) should do it .
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. Merge and Heap..which is really faster
    By silicon in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2005, 04:06 PM
  4. Insertion Sort Problem
    By silicon in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2005, 12:30 PM
  5. bit patterns of negtive numbers?
    By chunlee in forum C Programming
    Replies: 4
    Last Post: 11-08-2004, 08:20 AM

Tags for this Thread