Application of left and right shift of data in C language

Publisher:清新心情Latest update time:2015-07-01 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
I have been debugging a relatively complex program these two days. The source code is as follows:
unsigned long Peek(long address )
{
unsigned long value;
//if( address != PokePointer ) 
{SetAddress( address );}
//if( PeekPointer >= PeekLimit )
//{throw "Peek addressing error!";}

 
value += Read_Register( DATA_A ) << 24;
 
value += Read_Register( DATA_B ) << 16;
value += Read_Register( DATA_C ) << 8;
value += Read_Register( DATA_D );
//PeekPointer++; /* maintain local pointer */
return value;
}
The data returned by Read_Register( DATA_A ) are all byte type. I wonder if anyone can predict the result of the PPEK() function. Obviously, there is a low-level error in this function, because all bits of byte type data will become 0 after being shifted 8 bits to the left! Therefore, the PPEK function only returns Read_Register( DATA_D ); So what should the correct answer be? My correction is as follows:
 
value += Read_Register( DATA_A );
 
value =(value<<8) + Read_Register( DATA_B );
value =(value<<8) + Read_Register( DATA_C );
value =(value<<8) + Read_Register( DATA_D );
or:
value += (unsigned long) Read_Register( DATA_A ) << 24;
 
value += (unsigned long)Read_Register( DATA_B ) << 16;
value += (unsigned long)Read_Register( DATA_C ) << 8;
value += (unsigned long)Read_Register( DATA_D );
From the low-level mistakes I made above, I came to the conclusion that when writing code, when shifting left or right, you must pay attention to the type of the data being shifted, that is, the number of bits, to see if the operation will cause data overflow!
One more thing to add: the priority of left shift << and right shift >> is relatively low compared to mathematical operations. Let's take a look at the following expression:
c=a<<8+b; //Then this sentence means to shift a left by 8+b bits and assign it to c
Then if you want to shift a left by 8 bits and then add the value of b to assign it to c, you should write it as the following expression:
c=(a<<8)+b; 
 
The above are all low-level mistakes I have made. I hope those who see this can learn from them! ! !
Reference address:Application of left and right shift of data in C language

Previous article:A good single chip key processing subroutine
Next article:Talking about MCU packaging: Sometimes the product's exterior is just as important

Recommended ReadingLatest update time:2024-11-16 20:58

KEIL compiler distinguishes between RW-data and ZI-data
There is a statement "Program Size: Code=xx RO-data=xx RW-data=xx ZI-data=xx" in the compilation prompt output information of the KEIL project, which describes the size of each domain of the program. After compilation, all data with the same nature (including code) in the application program are classified into one do
[Microcontroller]
KEIL compiler distinguishes between RW-data and ZI-data
Serial DataFlash Memory and Its Interface with Microcontroller
1 Overview Flash memory can be divided into two categories according to its interface: serial and parallel. Serial Flash memory mostly uses I2C interface or SPI interface for reading and writing; compared with parallel Flash memory, it requires fewer pins, is small in size, is easy to expand, is simple to c
[Microcontroller]
Serial DataFlash Memory and Its Interface with Microcontroller
The trend of high quality and low price is obvious. Analysis of product performance and technical routes of mainstream collaborative robot manufacturers | MIR DATABANK
Collaborative robots are robots that can physically interact with humans in a shared working environment . They are designed to be flexible, safe and easy to use. They can improve production efficiency while maintaining collaborative interaction between humans and machines, changing the fixed operation
[robot]
Detailed explanation of idata, xdata, pdata, and data of 51 MCU
data: fixedly refers to the first 128 RAM from 0x00 to 0x7f, which can be directly read and written with acc, with the fastest speed and the smallest generated code. bit: refers to the bit addressable area from 0x20 to 0x2f idata: fixedly refers to the first 256 RAM from 0x00 to 0xff, of which the first 128
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号