Blue Europe Training--lessonc12[enumeration, bitwise operators, precompiled directives, Const]

Source: Internet
Author: User
Tags bitwise operators

//
Main.m
Lessonc12
//
Created by Zhang Yangji on 15-1-6.
Copyright (c) 2015 Zhang Yangji. All rights reserved.
//

#import <Foundation/Foundation.h>

#define Kdebug

When a variable has a fixed number of values, you can define an enumeration type
Enumeration types declare a set of constants that establish a correspondence between a person's readable identifier and a number that a computer can read.
If the enumeration type does not specify a constant, the default starts at 0, plus 1
Enum season{
Spring = 1,
Summer
Autumn = 4,
Winter
//};

typedef enum season{
Spring = 1 << 0,//1
Summer = 1 << 1,//2
Autumn = 1 << 2,//3
Winter = 1 << 3//4

}season;


typedef enum season season;




int main (int argc, const char * argv[]) {
Bitwise AND &, 0 is 0, all 1 is 1
Function: Some bits clear 0, need to clear 0 with 0 and, need to retain the bit with 1 and
int a = 0b11111111;
int c = A & 0b00001111;
printf ("%d", c);

Bitwise OR | , 1 is 1, all 0 is 0
Function: Put some binary position 1,
int b = 0b0110100;
int a = B | 0b00001111;
printf ("%d", a);

Bitwise counter ~, 1 is 0, 0 is 1
int a = 0b00000011;
int b = ~a;
printf ("%d", b);

Bitwise XOR or ^ differs by 1, same as 0
Used to record the difference between binaries
The function is the binary difference of the two numbers that participate in the operation or, when the two corresponding binary differences, the result is 1.

int a = 8,b = 7;
printf ("a =%d B =%d", a ^ (a ^ b), b ^ (a ^ b));
int a = 8,b = 7;
A = the difference of a ^ b;//a,b
b = a ^ b;//b = the difference between B and a A is worth the value of a
A = a ^ b;//a = B (now equals a value) and A/b difference is worth the value
printf ("a =%d B =%d", A, b);

<<, >> left shift, right shift operator
<<, move left one equivalent by 2;
>> shift right one equivalent to 2;
unsigned char a = 0b11000001;
A = a << 1;
unsigned char b = 0b11000001;
b = b >> 1;
printf ("%d", b);

Pre-compilation Directives
Conditional compilation: Compiling different instructions according to different conditions
1. [#ifdef] 2.[ifndef]3. [If]

int a = 10;
#ifdef Kdebug
printf ("%d", a);
#else
printf ("%d", A + 5);
#endif

#ifndef Kdebug
printf ("Hello");
#else
printf ("World");
#endif

#if 2
printf ("a");
#elif 1 + 2
printf ("B");
#elif 0
printf ("C");
#else
printf ("D");
#endif

Enumeration
SEASON A = Autumn | Winter
printf ("A =%d", a);
//
switch ((int) a) {
//
//
Case Spring | Summer
printf ("Spring or Summer");
Break
Case Spring | Winter
printf ("Spring or winter");
Break
Case Spring | Autumn
printf ("Spring or autumn");
Break
Case Summer | Autumn
printf ("Summer or Autumn");
Break
Case Autumn | Winter
printf ("Autumn or winter");
Break
//
Case Spring:
printf ("Spring");
Break
Case Summer:
printf ("Summer");
Break
Case Autumn:
printf ("Autume");
Break
Case Winter:
printf ("Winter");
Break
//
Default
printf ("Sorry");
Break
//    }
//

Const is used to decorate a variable that cannot be modified
const int a = 5;
Const-Modified array, in which every element in the array cannot be modified
const int A[3] = {1,2,5};

the int * Const P pointer cannot be redirected, but the pointer can be used to modify what the pointer points to
int a = 5, B = 9;
int * Const P = &a;
*p = 8;
*p = &b;
printf ("%d", *p);

the int const * p pointer can be redirected, but the pointer cannot be used to modify the pointing content
int a = 5, B = 8;
int const * p = &a;
*p = 5;//error
p = &b;
printf ("%d", *p);

the const int * p pointer can be redirected, but the pointer cannot be used to modify the pointing content
int a = 5, b = 5;
const INT * p = &a;
*p = 8;
p = &b;

the const int * Const P pointer cannot be re-directed, nor can it be modified by pointers
int a = 5, b = 4;
const INT * Const P = &a;

the int const * Const p pointer cannot be redirected, nor can the pointer be modified by pointers
int a = 5, b = 4;
int const * Const P = &a;

Const in the * before, through the pointer can not modify the point to the content, the pointer can be re-pointing
Const after *, the pointer cannot be redirected, but the content can be modified by pointers

return 0;
}


This article is from the "ios--Training" blog, so be sure to keep this source http://zys2007.blog.51cto.com/8207599/1600522

Blue Europe Training--lessonc12[enumeration, bitwise operators, precompiled directives, Const]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.