Thread: working with binaries

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    52

    working with binaries

    I'm learning how to work with binaries in C.

    I tested this code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      int x=3;
      x = x &#187; 1;
      printf("lol: &#37;d",x);
      system("PAUSE");	
      return 0;
    }
    But u get the next error in the line "x = x &#187; 1;"

    stray '\187' in program
    I was expecting that, if x = 3 (11) that x&#187;1 would make x = 1 (01).
    What am i doing wrong here?

    Edit:
    If i have x = ~x, then x=3 (11) will be -4 and not 0 (00), why?
    Last edited by NeMewSys; 06-10-2008 at 07:53 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You should use two > signs, not the single character that looks like two at once.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by NeMewSys View Post
    Edit:
    If i have x = ~x, then x=3 (11) will be -4 and not 0 (00), why?
    Because it inverts all bits.
    3 = 0000 0011
    So ~3 is 1111 1100
    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.

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    You're using French-style closing speech marks. You need two greater-than symbols.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    52
    Quote Originally Posted by Elysia View Post
    Because it inverts all bits.
    3 = 0000 0011
    So ~3 is 1111 1100
    Wait, i got confused now about a thing, in your example you show the int x with 8 bits

    0000 0000 = 8bits

    Does int has 8 bits or 16 bits?
    wich bit is the "negative flag" of the int?

    Ps. I loled when you told me it was >> not », xD funny errors.

    Thanks!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Obviously, int can be 16 bits or 32 bits depending on the system, but the same theory applies there. All 0s become 1s and all 1s become 0s.
    And the sign bit is the highest bit (bit 16 or 32).

    16 bits (3): 0000 0000 0000 0011
    16 bits (~3): 1111 1111 1111 1100
    32 bits (3): 0000 0000 0000 0000 0000 0000 0000 0011
    32 bits (~3): 1111 1111 1111 1111 1111 1111 1111 1100
    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.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    52
    One last thing, in all the examples where i see that the programmer compares the last bit of X with 1, they do it this way:


    if ( x & 01) ...
    in php i used to do this way if (x & 1)...
    are they the same?

    Thanks!

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, they are the same, except that 01 is in octal.
    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

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    1 = decimal (base 10)
    01 = octal (base 8)
    0x1 = hexdecimal (base 16)

    When working with binary, we usually use hexdecimal because of its close relationship with binary (base 2).
    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.

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    52
    Every number that begins with a zero is octal by default?

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    By definition (C's definition), anyway. If you try to type "08" in a program, bad things will happen.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it's quite silly, really, but that's the way it was done.
    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.

  13. #13
    Registered User
    Join Date
    May 2008
    Posts
    52
    Quote Originally Posted by tabstop View Post
    By definition (C's definition), anyway. If you try to type "08" in a program, bad things will happen.
    that was my fear.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, 08 is illegal because 8 cannot be represented in base 8, so it would just give a compile error.
    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.

  15. #15
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    Yes, it's quite silly, really, but that's the way it was done.
    Why is it silly?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM