Some random C questions (ascii magic and bitwise operators)

I am trying to learn C programming, and I was studying some source codes, and there are some things that I did not understand, especially regarding Bitwise operators. I read some sites about this, and I have an idea of ​​what they do, but when I returned to look at these codes, I could not understand why and how they are used there.

My first question is not related to bitwise operators, but rather the magic of ascii:

  • Can someone explain to me how the following code works?

    char a = 3;
    int x = a - '0';
    

    I understand that this is done to convert char to int, but I don't understand the logic behind it. Why / How does it work?

  • Now, As for bitwise operators, I feel really lost here.

    • What does this code do?

      if (~pointer->intX & (1 << i)) { c++; n = i; }
      

      - , ~ , , .

      :

      row.data = ~(1 << i);
      
    • :

      if (x != a)
        {
          ret |= ROW;
        }
      

      | =? , , | = is OR, , .

      , , ? , , , , - , !


, .

: appartenly , " " , , , "bitlevel". ?

+5
10

:

char a = 3; 
int x = a - '0';

- :

char a = '3'; 
int x = a - '0';

char , . 0 9 , , "0" "9", 9. , .

(~pointer->intX & (1 << i))

if true, . .

~ , pointer->intX 01101010, ~pointer->intX 10010101. ( , , . 32- , 32 1 0).

, . 1, 1. , 00101001, 00001011, 00001001.

, << . 00000001 , 00001000. , (1 < i) , , .

, , i () pointer->intX.

, , ~(1 << i). i 4, 00010000, 11101111.

ret |= ROW;

:

ret = ret | ROW;

| &, , 1, 1. , ret - 00100000, ROW - 00000010, 00100010.

+17

ret |= ROW;

ret = ret | ROW;

0

, , char. '0' char '0', ASCII- 48. 3-'0' = 3-48

'1 < i' 1 , i- 1.
~ pointer- > intX intX, AND ( 0), intX , i- , .

0
char a = '3';  
int x = a - '0';

( 3), ascii 3 char, "3" - "0" x, - , ascii, x 3 ( )

, , ~ , , ? :

(~pointer->intX & (1 << i))

"( intX ) (1 )"

1 < - 1 2, .. 3, 1 < 3 == 8

, ..

x | = y x = x |

0

char a = 3; int x = a - '0'; , char a = '3'; int x = a - '0';. , , ASCII , "0", "1", "2",... , "0" 48, "1" 49, "1 '-' 0 ' 1.

, . , , ...

010 & 111 = 010
010 | 111 = 111
010 ^ 111 = 101
~010 = 101
0

, , , , :

char a = '3';

, , "0" - . , '0' - '0' = 0. '1' - '0' = 1, '1' , '0'. Etc.

0

1) A char 8- . '0' == 48, , .

2) (~ (pointer- > intX) (1 < i)) , i- () intX . ~ , 0s 1s , 1 < 1 , , , 0.

3) | . , , , . 0b11000000 | 0b00000011 == 0b11000011. | = - , , a + = b a = a + b, a | = b a = a | b.

, , , .

0

, , , ASCII , . , ASCII '0' - 48 ( ), "1" - 49, "2" 50 .. ASCII ('1') - ASCII ('0') = 49 - 48 = 1.

, .

:

(1 << i) - 1 . , = 0, . = 1, , , 0010 2. = 2, , , 0100 4 ..

~pointer->intX - intX- , .

& - . 1, , 1 0 .

, , pointer->intX 0 i- .

, |= OR . 1 , 1,

0

1) - , ? char a = 3; int x = a - '0'; , char int, , . / ?

. a char 0, C char. , , x .

2) , , . --- ? if (~ pointer- > intX (1 < i)) {++; n = i; } - , ~ , , .

(~pointer->intX & (1 << i)) is saying:

intX, AND 1

, , intX = 1011 = 2,

(0100 & 0100) 

-negate 1011 = 0100

-(1 << 2) = 0100

0100 & 0100 = 1 :)

, AND 1 (, , ) {++; n = i; }

, c 1, n i

: row.data = ~ (1 < i);

Same principle here.
Shift a 1 to the left by i places, and negate.

So, if i = 2 again

(1 << 2) = 0100

~(0100) = 1011

** --- :

if (x != a) { ret |= ROW; }

exacly | =? , , | = is OR, , . **

if (x!= a) (, .... x a)

ret |= ROW;

equates to

ret = ret | ROW;

OR ret ROW

, , .

... ..

0
  • , char a = '3'; ( ). , ASCII , "0" = 48, "1" = 49 .. "0" ASCII , , , '3' - '0' = 3 .. , , , "0" , , .

  • . "" . , , , if , - > intX , 0. , ~, ~ . , . , , , .

    . EXCEPT i, 1. (1 < i).

  • The bitwise OR operation in this case is used to set the bits specified by the ROW constant to 1. If these bits are not set, they set them; if they are already installed, this does not affect.

0
source

All Articles