Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

0x in binary

ryantcbryantcb Posts: 667Registered Users @ @ @
Im reading some sample code that has
#define BACON_CATEGORY   (0x00000001)
#define FRUIT_CATEGORY    ((0x00000001)<<1)
#define BADDIE_CATEGORY  ((0x00000001)<<2)

Now in binary these are obviously values 1, 2 and 4, but I see the 0x at the start which I thought was an indicator of hexidecimal numbers. What does the 0x indicate in binary?

Replies

  • givensurgivensur On the Information SuperhighwayPosts: 541Registered Users @ @ @
    In C (and Objective-C), 0x does indicate hexadecimal.

    Bacon is 1, Fruit is 2 because it starts as 1 and gets shifted over a bit, and Baddie is 4 because it starts as 1 and gets shifted over 2 bits.

    Binary numbers start with 0b, although that is only a recent addition to the language. So the same thing could be written as 0b1, 0b10, and 0b100.
    I make cake apps.
  • ryantcbryantcb Posts: 667Registered Users @ @ @
    Ok Thank you. What would be the reason to use 0x00000001 and not just 0x1?
  • givensurgivensur On the Information SuperhighwayPosts: 541Registered Users @ @ @
    ryantcb wrote: »
    Ok Thank you. What would be the reason to use 0x00000001 and not just 0x1?
    Just for formatting reasons I guess. I prefer the shorter way myself.
    I make cake apps.
  • TunaNuggetTunaNugget Posts: 1,123Registered Users @ @ @ @
    edited November 2013
    1 is 1 in every base. The formatting here is a harmless brain fart.
Sign In or Register to comment.