Thread: Binary numbers

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Binary numbers

    I was just thinking, why can't you use binary numbers like this: 0b11010101, when you can use hexadecimal numbers 0xF7A0??? Is there some reason for this?
    This would make bitmasking easier...

    (MyFlags & 0b00000101)

    instead of

    (MyFlags & 5)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Well you could always write an inline function to convert bitfields to numbers, but for most people hex representation is adequate, do enough bitwise work and you eventually memorize the nibbles.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >This would make bitmasking easier...
    Perhaps for small values, but it isn't practical when the values get larger than 8 bits. Would you rather write this:

    mask &= 0b0100000000000000

    or this:

    mask &= 0x4000

    Looks like no contest to me, it's confusing enough with just a 16 bit binary value. Can you be sure that it's really 16 bits without counting? What if I needed a high bit on a 64 bit value? Would you like to maintain code with that? It's just too error prone to have any real use.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. checking if binary numbers are palindromic
    By Beatz in forum C Programming
    Replies: 3
    Last Post: 01-24-2008, 01:49 PM
  2. something about binary numbers in c
    By louis_mine in forum C Programming
    Replies: 1
    Last Post: 02-09-2005, 10:22 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Binary representation of numbers...
    By roc in forum C++ Programming
    Replies: 2
    Last Post: 05-14-2003, 07:42 PM
  5. how to pack 8 x 4 bit binary numbers into a long int?
    By n00bcodezor in forum C Programming
    Replies: 11
    Last Post: 11-19-2001, 05:46 PM