C Language Shift operation

Source: Internet
Author: User
Tags arithmetic

Should first look at the C language refers to all the bit binary arithmetic bit calculation. Even if the input is a decimal number, the memory is stored in binary form. "<<" Method of Use: The format is: A<<m,a and M-type, requires m>=0. Function: The integer number a press bits to the left to move the M-bit, after the high position removed, the low 0. The ">>" method of Use: The format is: A>>m,a and M must be an integral type expression. Request m>=0.

Function: The integer number a press bits to the right to move the M bit, after the low shift out, high 0

Shift operations in the C language. Not much content. Just some places you don't pay attention to, you neglect.
Gossip less say, first do two small questions first.
(1) unsigned char x=3;
How much is x<<1? How much is x>>1?
(2) Char x=3;
How much is x<<1? How much is x>>1?
(3) Char x=-3;
How much is x<<1? How much is x>>1?

3 written in binary number is 00000011. -3 written in binary number is (complement) 11111101.
When the program runs. An encoded representation of a numeric value is manipulated. That is, the binary representation of the value in memory. Say
When the program takes 3, it takes 11111101.

(1) to the unsigned number, said. X<<1 to the left one, the leftmost displacement is dropped. The rightmost bit to move in is 0. Become
00000110, so the result is 6. X>>1 one to the right. Because it is an unsigned number, the logic shifts to the right, and the rightmost one moves away,
The leftmost bit to move in is 0. becomes 00000001, so the result is 1.


(2) for the signed number three said, X<<1 to the left one. The leftmost displacement is dropped, and the rightmost bit of the move in is 0. Become
00000110, so the result is 6. X>>1 to the right, because it is a signed number, a logical right shift may occur, or an arithmetic right shift can occur.
, this point. The C standard does not clearly specify whether to use logical right SHIFT or arithmetic right shift.

But most machines use arithmetic to move right, to become
00000001, so the result is still 1.

But please note that this is just saying that most of the machines are this, you dare to guarantee yourself
No special circumstances?
(3) for the signed number--the said. X<<1 to the left one, the leftmost displacement is dropped, the rightmost move in the bit to fill 0.

Become
11111010, the result is-6.

Move one bit to the right because it is a signed number. A logical right shift may occur, or arithmetic right shifts can occur.
Most machines use arithmetic to move right. becomes 11111110, the result is-2.

Summary: Always shift and fill 0 when moving left. When moving right, the unsigned number is shift and complement 0, which is called logical right shift.
The number of symbols in most cases is the shift and fill the leftmost bit (that is, the most significant bit), move a few to fill several, this is called arithmetic right shift.

Code with byte encoding in print memory:
void Print_char (char x)
{
unsigned char * bp= (unsigned char *) &x;
int size=sizeof (x);
for (int i=0; i<size; i++)
printf ("%.2x", Bp[i]);
printf ("/n");
}
This practice can be practiced on your own.

From quote: http://blog.chinaunix.net/u1/33888/showart_334911.html

C Language Shift action

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.