constructing 32 bit unsigned int in c

user2785532

I am trying to construct a 32 bit binary value using an unsigned int. This is how the 32 bit is split into parts

part 1 = 4 bit

part 2 = 2 bit

part 3 = 12 bit

part 4 = 2 bit

part 5 = 12 bit

How do i use the parts to construct 32 bit binary value (unsigned int) using bit shifting can someone please help

(cant just shift bits... what if i want to update certain part?)

Andrew Cottrell

This is typical bit-shifting/bit-masking stuff. Here's how you set the initial value:

uint32_t value = ((part1 & 0xF) << 28)
                 | ((part2 & 0x3) << 26)
                 | ((part3 & 0xFFF) << 14)
                 | ((part4 & 0x3) << 12)
                 | (part5 & 0xFFF);

Each line uses a bitwise-AND (&) to clear the upper bits of each part so it doesn't overflow its allocated bit-width within the final value. For example, if part4 was 0xFF and you forgot the & 0x3 then the upper 6 bits of part4 (0xFC) would spill into the region for part3. Then the part is shifted (<<) to its final location and bitwise-OR'd (|) with the rest of the parts.

Some developers accomplish the same thing via bitfields but I don't recommend that approach due to potential portability issues.

Most (all?) of the other answers here so far have forgotten the bitwise-AND portion of the solution. Their answers will result in bugs if the part values ever exceed the specified bit width.

If you want to update a particular portion of the value, you'll need some more bit-masking via bitwise-AND and bitwise-OR. For example, to update part4 you'd do this:

value &= ~(0x3 << 12);  /* Clear the part4 region */
value |= (part4 & 0x3) << 12;    /* Set the part4 region to the new value */

That first line is a little tricky if you're new to bitwork in C. It says take 0x3 and shift it by 12 (result = 0x00003000), perform a bitwise-complement (result = 0xFFFFCFFF), and set value equal to itself bitwise-AND'd with that result. That's how you clear the part4 region of the value... because you're bitwise-AND'ing that region with zero the result is that region is now zero.

The second line sets the zeroed part4 region to the new value, just like we did above when setting the initial value.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Converting 16 bit unsigned int array to 32 bit float array

From Dev

Unsigned int from 32 bit to 64bit OS

From Dev

use of 128 bit unsigned int in c language

From Dev

Combine four 8-bit unsigned ints into one 32-bit unsigned int

From Dev

Combine four 8-bit unsigned ints into one 32-bit unsigned int

From Dev

How to split an unsigned long int (32 bit) into 8 nibbles?

From Dev

Display a 32 bit unsigned int image with Tkinter and Python 3

From Dev

C - Unsigned long long to double on 32-bit machine

From Dev

Vector of 64-bit double faster to dot-product than a vector of 32-bit unsigned int?

From Dev

Vector of 64-bit double faster to dot-product than a vector of 32-bit unsigned int?

From Dev

Extracting bits from a 64 bit hex address into an unsigned int in C

From Dev

C and C++ odd syntax "unsigned int :32;"

From Dev

bit representation of unsigned int a = -1

From Dev

Setting nth bit of unsigned int

From Dev

C unsigned int + int

From Dev

Why can't I flip the bits of a 32 bit unsigned int by bitwise ORing it with 0?

From Dev

How to convert integer to unsigned 32 bit in python?

From Dev

Why do bit fields in C need to be defined of type unsigned int or signed int

From Dev

Can someone explain the meaning of malloc(20 * c | -(20 * (unsigned __int64)(unsigned int)c >> 32 != 0))

From Dev

3 bit unsigned integer in c

From Dev

migrationg a hash function with 32 bit unsigned integers from c++/Qt to java

From Dev

C: How to save the result of 2 32 bit unsigned integer into a signed integer

From Dev

Interpreting a 32bit unsigned long as Single Precision IEEE-754 Float in C

From Dev

Is there a non-looping unsigned 32-bit integer square root function C

From Dev

Behavior of unsigned int in C

From Dev

C# change the first 32bit Int of a GUID

From Dev

Convert int to 16 bit unsigned short

From Dev

bit shift for unsigned int, why negative?

From Dev

Unsigned int bitmask for multiple bit positions

Related Related

  1. 1

    Converting 16 bit unsigned int array to 32 bit float array

  2. 2

    Unsigned int from 32 bit to 64bit OS

  3. 3

    use of 128 bit unsigned int in c language

  4. 4

    Combine four 8-bit unsigned ints into one 32-bit unsigned int

  5. 5

    Combine four 8-bit unsigned ints into one 32-bit unsigned int

  6. 6

    How to split an unsigned long int (32 bit) into 8 nibbles?

  7. 7

    Display a 32 bit unsigned int image with Tkinter and Python 3

  8. 8

    C - Unsigned long long to double on 32-bit machine

  9. 9

    Vector of 64-bit double faster to dot-product than a vector of 32-bit unsigned int?

  10. 10

    Vector of 64-bit double faster to dot-product than a vector of 32-bit unsigned int?

  11. 11

    Extracting bits from a 64 bit hex address into an unsigned int in C

  12. 12

    C and C++ odd syntax "unsigned int :32;"

  13. 13

    bit representation of unsigned int a = -1

  14. 14

    Setting nth bit of unsigned int

  15. 15

    C unsigned int + int

  16. 16

    Why can't I flip the bits of a 32 bit unsigned int by bitwise ORing it with 0?

  17. 17

    How to convert integer to unsigned 32 bit in python?

  18. 18

    Why do bit fields in C need to be defined of type unsigned int or signed int

  19. 19

    Can someone explain the meaning of malloc(20 * c | -(20 * (unsigned __int64)(unsigned int)c >> 32 != 0))

  20. 20

    3 bit unsigned integer in c

  21. 21

    migrationg a hash function with 32 bit unsigned integers from c++/Qt to java

  22. 22

    C: How to save the result of 2 32 bit unsigned integer into a signed integer

  23. 23

    Interpreting a 32bit unsigned long as Single Precision IEEE-754 Float in C

  24. 24

    Is there a non-looping unsigned 32-bit integer square root function C

  25. 25

    Behavior of unsigned int in C

  26. 26

    C# change the first 32bit Int of a GUID

  27. 27

    Convert int to 16 bit unsigned short

  28. 28

    bit shift for unsigned int, why negative?

  29. 29

    Unsigned int bitmask for multiple bit positions

HotTag

Archive