Chanyoung Nathan Lee
Chanyoung Nathan Lee

Reputation: 45

int array regarding binary digits

I am wondering why the results turn out to be 100, 8, 1 in the output. 010 doesn't even make any sense that it is not 2 in binary digits. Am I missing something?

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
    int code[3];
    code[0] = 100;
    code[1] = 010;
    code[2] = 001;
    printf("%d\n", code[0]);
    printf("%d\n", code[1]);
    printf("%d\n", code[2]);
}

Upvotes: 0

Views: 55

Answers (1)

meat
meat

Reputation: 619

100, 010 and 001 are not binary literals. 100 is a decimal literal; 010 and 001 are octal (base 8) literals. If you are using gcc there is an extension to support binary literals by using the prefix 0b, as in

code[0] = 0b100; // evaluates to 4

Upvotes: 1

Related Questions