Thread: Binary representation in C

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    I don't know but it looks like it takes some time to become second nature. knowing that 0101 is 5 can be done easily. but when you have a whole 32 digit binary number it gets a bit hard locating the bits you want.

    What I mean is that from Hex to Binary you can get used to it. but the opposite can be very complicating

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, and telling the difference between a 1 with 30 zeros and a 1 with 31 zeros can only be resolved by counting them [unless the numbers are right next to each other]. The difference between 0x40000000 and 0x80000000 is pretty immediately noticable.

    --
    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. #18
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Theres an 0b prefix in the GNU C language extensions.

  4. #19
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by matsp View Post
    Yes, and telling the difference between a 1 with 30 zeros and a 1 with 31 zeros can only be resolved by counting them [unless the numbers are right next to each other]. The difference between 0x40000000 and 0x80000000 is pretty immediately noticable.
    Real "structured assembly":
    Code:
              [p1]    = 00010000B           ;initalize port before setting direction
              [pd1]   = 00011110B
              ;         +|||||||            I  CELL_MOD_ON_STATE/INT (int5l)
              ;         |+||||||            I  RF2400_STATUS/INT  (int4l, pullup)
              ;         ||+|||||            I  EBRD_INT (int3l, pullup)
              ;         |||+||||            OH SERIAL_FLASH_CS (low true)
              ;         ||||+|||            OL RAW_SW_ENA (also enable 5v,  hi true)
              ;         |||||+||            OL BATT_CHARGE (hi true, set BATT_LOAD=0 first!!)
              ;         ||||||+|            OL BATT_LOAD (hi true, set BATT_CHARGE=0 first!!)
              ;         |||||||+            I  PHONE_LINE_DET
    Such a thing can be useful.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #20
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Abda92 View Post
    I don't know but it looks like it takes some time to become second nature. knowing that 0101 is 5 can be done easily. but when you have a whole 32 digit binary number it gets a bit hard locating the bits you want.

    What I mean is that from Hex to Binary you can get used to it. but the opposite can be very complicating
    If you don't write numbers out in binary then you don't have this problem.

  6. #21
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >          [pd1]   = 00011110B
    Can you explain what this line does? It doesn't seem to match the comments, as this would set both BATT_CHARGE and BATT_LOAD.

  7. #22
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by swoopy View Post
    Code:
    >          [pd1]   = 00011110B
    Can you explain what this line does? It doesn't seem to match the comments, as this would set both BATT_CHARGE and BATT_LOAD.
    Port value setting is followed by port direction setting.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #23
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Every one has an opinion on such topics so I guess I'd like to also give mine.
    binary is a base 2 alphabet that's been over extended in various ways to cater for much more complexity. I'm of the opinion that it's better left out or left to you the programmer to manipulate the machine language as you see fit. Octal and hex are extended binary with increased base to handle several real world complexities. why have binary notation when you may as well just use assembly notation to show you wish to work with native machine code ( asm )? also think word boundaries and the like. I believe, rather than worry about havein binary notation we should be looking at creating base32 and 64 notations so that we can startd doing more with one letter.
    Imagine dealing with huge numberes in base two? (not fun for the mind)
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  9. #24
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Dave_Sinkula View Post
    Port value setting is followed by port direction setting.
    Ok, it took me a while to interpret. I believe I understand. You are referring to this:
    Code:
    I
    I
    I
    O
    O
    O
    O
    I

  10. #25
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I think the b notation (101101b) deserves to become a standard, just like 0x. I probably wouldn't use it very often; but I'm sure I'd find it handy every now & then.

    As for bit flags, these are all the numbers you need to "memorize":
    0x1
    0x2
    0x4
    0x8

    All flags larger than that are just shifted to the left:
    0x10, 0x20, 0x40, 0x80, 0x100, 0x200, 0x400, 0x800...

  11. #26
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by swoopy View Post
    Ok, it took me a while to interpret. I believe I understand. You are referring to this:
    Code:
    I
    I
    I
    O
    O
    O
    O
    I
    Aye. Just a random C&P.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-25-2006, 04:14 PM
  2. Binary Representation Code
    By passionate_guy in forum C Programming
    Replies: 6
    Last Post: 01-22-2006, 02:31 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Binary representation similar to 0xFF ?
    By DougDbug in forum C++ Programming
    Replies: 3
    Last Post: 01-10-2003, 12:08 PM