Thread: Problems with bit manipulation

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    6

    Problems with bit manipulation

    I was trying to get a variable to represent all 1's. here is my code:

    unsigned long long x;
    x = ~0;

    cout << x << endl;

    when x is printed, it give me 18446744073709551615. I plug that into a calculator and it spits out a 1 followed by 64 zeros. Number 1. I thought long long was 64 bits (on a 32 bit machine) and not 65 and 2. why is it not all 1's? Is x the correct value and I just am not printing it out correctly?
    Last edited by redhawk87; 09-21-2011 at 09:20 PM.

  2. #2
    Registered User
    Join Date
    Sep 2011
    Posts
    6
    ok, I tried printing it out using printf("%x", x) and printf("%d",x) and got 0xFFFFFFFF and -1 respectively. So it looks like it is working. So my question is, why does cout print a wrong value? Also does printf only print 32 bits? If so how can I get all 64 bits printed fairly easily?

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by redhawk87
    when x is printed, it give me 18446744073709551615. I plug that into a calculator and it spits out a 1 followed by 64 zeros.
    I plugged that into my calculator and it spits out 64 1s for the binary representation. Logically, looking at the decimal representation, this number must be odd, so it is impossible for the last bit to be a 0. I suspect a data entry error.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Bit Manipulation
    By Sicilian_10 in forum C Programming
    Replies: 4
    Last Post: 03-19-2010, 10:01 AM
  2. string manipulation problems
    By stanlvw in forum C++ Programming
    Replies: 19
    Last Post: 06-09-2008, 02:10 PM
  3. Bit Manipulation???
    By John_L in forum C Programming
    Replies: 1
    Last Post: 11-28-2007, 01:06 AM
  4. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  5. problems with string manipulation
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 04:45 PM