This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get the value of individual bytes of a variable?
How can I get single bytes from a word (for example an unsigned int in C) without using bit-wise operations (that is using arithmetic operations?)
I don't know why, but this formula (C-Like) for an x number doesn't seem to work:
floor(x / pow(R, i)) % R
where R is the radix with which the number is represented, and i is used to indicate the i-th byte to obtain.
If you really need to avoid bitwise operations, you can cheat alternatively (beware if you're using a little or big endian machine!):
char *int_16_storage;
uint16_t the_word = 0xabcd;
int_16_storage = &the_word;
uint8_t low_byte = int_16_storage[0];
uint8_t high_byte = int_16_storage[1];
Related
This question already has answers here:
What is bit masking?
(2 answers)
Closed 1 year ago.
A struct I'm dealing with has a field defined as uint8_t. The spec also states that all multiple-byte fields are represented in host-endian format.
Bits 0:3 contain the information that I need (also an unsigned integer). Using plain C, how do I extract those 3 bits and convert it to a number type?
unit8_t a;
unit8_t b;
a = input data;
b = a & 0x0F; // b contains a number from 0 to 15
This question already has answers here:
Algorithm to generate bit mask
(9 answers)
Closed 4 years ago.
Problem:
I want to generate a Bit mask (uint32_t) based on a given length.
Following result should be achieved:
BIT_MASK(3) = 0x00..0111
BIT_MASK(32) = 0x111..111
The code given below is working for every length smaller than 32. If the length is 32, the left shift count is larger than the type width (overflow).
#define BIT(n) ( 1<<(n) )
#define BIT_MASK(len) ( BIT(len)-1 )
uint32_t length;
uint32_t mask = BIT_MASK(length);
Question:
Is there any other efficient macro solution to generate a Bit mask, which is not including an additional if/else or typecast to avoid that error.
This is pretty much it, but you need to change the literal 1 to 1UL. Otherwise you are restricted to the range of int, which is likely 31 bits instead of 32.
#define BIT_MASK32(n) ( (1UL<<(n)) - 1UL )
Where n must be in the range of 0 to 31 to fit a uint32_t. To safe guard against overflow, you could make it (n)%32, though that will make the macro slower if n is a run-time value rather than an integer constant.
This question already has answers here:
What is the fastest/most efficient way to find the highest set bit (msb) in an integer in C?
(31 answers)
Closed 6 years ago.
I have binary numbers (32 / 64 bit length) which I want to shift to the left so that the leading significant bit disappears. All the binary numbers are of different length. So I did not find an easy way to do it.
Here are some examples. On the left side are the binary numbers before and on the right side the binary numbers after the left shift:
00000000000000000000000000101011011 -> 01011011000000000000000000000000000
00000000000000000000100010110111011 -> 00010110111011000000000000000000000
00000000000000000000000000000100110 -> 00110000000000000000000000000000000
00000000000000111000101010101100010 -> 11000101010101100010000000000000000
How can one do that in C?
uint64_t number;
for(int i = 0; (i < 64) && (number & 0x8000000000000000) == 0); i++)
{
number <<= 1;
}
number <<= 1;
This is one way of doing it for 64 bit number value;
Fixed.
This question already has answers here:
What does least significant byte mean?
(4 answers)
Closed 7 years ago.
I am working on an assignment and it is asking me to calculate a checksum by stripping off the least significant byte of a ones complement version of an integer...
This is the part of the assignment outline I am confused by:
"The CHECKSUM field (MM) value is calculated by taking the least significant byte of the 1’s Complement value of the sum of the COUNT, ADDRESS
and DATA fields of the record"
Im a little bit unclear on what this means, as I haven't really worked with ones complements or LSB's in C.
What I have so far is:
int checkSum(int count, int address, char* data)
{
int i = 0;
int dataTotal = 0;
for(i = 0; i < strlen(data); i += 2)
{
dataTotal += (getIntFromHex(data[i]) * 16) + getIntFromHex(data[i + 1]);
}
int checksum = ~(count + address + dataTotal) & 1;
printf("Checksum: %.2X\n", checksum);
return checksum;
}
I didn't really expect this to work but I've done some research and this is what I came up with.
I need some clarification on what is meant by the least significant byte.
P.S. The reason for the for loop is simply just to get the total of the data. Not important for this but the code uses the variable so I figured I would just copy the whole thing to avoid confusion.
I need some clarification on what is meant by the least significant byte.
The last significant byte means the number mod 256, a result from zero to 255.
unsigned leastSignificantByte(unsigned j)
{
return j & 0xff;
}
So Im using this (its from another question I did),
unsigned char *y = resultado->informacion;
int i = 0;
int tam = data->tamanho;
unsigned char resAfter;
for (int i=0; i<tam;i++)
{
unsigned char x = data->informacion[i];
x <<= 3;
if (i>0)
{
resAfter = (resAfter << 5) | x;
}
else
{
resAfter = x;
}
}
printf("resAfter es %s\n", resAfter);
so at the end I have this really long number (Im estimating about 43 bits), how can I get groups of 8 bits, I think im gettin something like (010101010101010.....000) and I want to separate this in groups of 8.
Another question, I know for sure that resAfter is going to have n number of bits where n is a multiply of 8 plus 3, so my question is: is this possible? or c is going to complete the byte? like if I get 43 bits then c is going to fill them with 0 and complete so I have 48 bits; and is there a way to delete these 3 bits?
Im new on c and bitwise so sorry if what Im doing is reallly bad.
Basically in programming you deal with bytes (i think, at least in most cases), in C you deal with types of specific size (depending on system you run it on).
That said char usually has size of 1 byte, and I don't really think you can playing around with single bits. I mean u can do operation on them (<< for instance) in scale of single bits but i don't know of any standard way to preserve less than 8 bits in variable in C (though i may be wrong about it)