Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ shifting bits

I am new to shifting bits, but I am trying to debug the following snippet:

if (!(strcmp(arr[i].GetValType(), "f64")))
 {
        dem_content_buff[BytFldPos] = tmp_data;
     dem_content_buff[BytFldPos + 1] =  tmp_data >> 8;
     dem_content_buff[BytFldPos + 2] = tmp_data >> 16;
     dem_content_buff[BytFldPos + 3] = tmp_data >> 24;
     dem_content_buff[BytFldPos + 4] = tmp_data >> 32;
     dem_content_buff[BytFldPos + 5] = tmp_data >> 40;
     dem_content_buff[BytFldPos + 6] = tmp_data >> 48;
     dem_content_buff[BytFldPos + 7] = tmp_data >> 56;
        }    

I am getting a warning saying the lines with "32" to "56" have a shift count that is too large. The "f64" in the predicate just means that the data should be 64bit data.

How should this be done?

edit:

I should have put more of the code in.

tmp_data = simulated_data[index_data];

if (!(strcmp(dems[i].GetValType(), "s32")))

{ dem_content_buff[BytFldPos] = tmp_data; dem_content_buff[BytFldPos + 1] = tmp_data >> 8; dem_content_buff[BytFldPos + 2] = tmp_data >> 16; dem_content_buff[BytFldPos + 3] = tmp_data >> 24;
}

if (!(strcmp(dems[i].GetValType(), "f64"))) { dem_content_buff[BytFldPos] = tmp_data; dem_content_buff[BytFldPos + 1] = tmp_data >> 8; dem_content_buff[BytFldPos + 2] = tmp_data >> 16; dem_content_buff[BytFldPos + 3] = tmp_data >> 24; dem_content_buff[BytFldPos + 4] = tmp_data >> 32; dem_content_buff[BytFldPos + 5] = tmp_data >> 40; dem_content_buff[BytFldPos + 6] = tmp_data >> 48; dem_content_buff[BytFldPos + 7] = tmp_data >> 56; }

So, dem_content_buff right now only holds ints. Can I not use this array for the 64 bit data either?

like image 744
Blade3 Avatar asked Feb 26 '26 21:02

Blade3


1 Answers

What is the type of tmp_data?

My guess is that tmp_data is only 32-bits, hence the error. It will need to be a 64-bit unsigned int, which is can be achieved using the non-standard (but well-supported) unsigned long long data type.

like image 96
Peter Alexander Avatar answered Feb 28 '26 13:02

Peter Alexander