set most significant bit in C

Taylor

I am trying to set the most significant bit in a long long unsigned, x. To do that I am using this line of code:

x |= 1<<((sizeof(x)*8)-1);

I thought this should work, because sizeof gives size in bytes, so I multiplied by 8 and subtract one to set the final bit. Whenever I do that, the compiler has this warning: "warning: left shift count >= width of type"

I don't understand why this error is occurring.

AnT

The 1 that you are shifting is a constant of type int, which means that you are shifting an int value by sizeof(unsigned long long) * 8) - 1 bits. This shift can easily be more than the width of int, which is apparently what happened in your case.

If you want to obtain some bit-mask mask of unsigned long long type, you should start with an initial bit-mask of unsigned long long type, not of int type.

1ull << (sizeof(x) * CHAR_BIT) - 1

An arguably better way to build the same mask would be

~(-1ull >> 1)

or

~(~0ull >> 1)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find most significant set bit in a long

From Dev

Fast way of finding most and least significant bit set in a 64-bit integer

From Dev

Create a mask that marks the most significant set bit, using only bitwise operators

From Dev

How to clear most significant bit in byte?

From Dev

Comparing the Most Significant Bit of two numbers: ==, <, <=

From Dev

Get the most significant bit from an 8-bit value

From Dev

Get the most significant bit from an 8-bit value

From Dev

Index of second least significant set bit

From Dev

Efficient least significant set bit of "biginteger" class

From Dev

how is the most significant bit radix sort more efficient than the least significant bit radix sort?

From Dev

The MSB (most significant bit ) in pyserial python sent to arduino uno in damged

From Dev

How do I flip the most significant bit in MIPS?

From Dev

How to get position of right most set bit in C

From Dev

Writing a bit reader in JAVA (32-bit little-endian most-to-least-significant bit packing)

From Dev

Fastest way to get last significant bit position in a ulong (C#)?

From Dev

How to write a constant time function to copy the most significant bit to all bits

From Dev

How to change the most significant bit to a 1 after shifting an int to the right using '>>'?

From Dev

How to interpret using Most Significant Bit of single byte - Pcapng time format "if_tsresol"

From Dev

Most Significant Byte Calculation

From Dev

Hex of most significant nibble

From Dev

Xor starting with the significant bit

From Dev

Clearing least significant bit

From Dev

Least significant bit mips

From Dev

C set a bit (bit manipulation)

From Java

Why is the segfault address NULL when accessing memory that has any of the 16 most significant bits set?

From Dev

Get most significant digit in python

From Dev

Getting least significant bit in JavaScript

From Dev

Extraction of the least significant bit of a pixel

From Dev

How to extract digits from a number in C? Begining from the most significant digit?

Related Related

  1. 1

    Find most significant set bit in a long

  2. 2

    Fast way of finding most and least significant bit set in a 64-bit integer

  3. 3

    Create a mask that marks the most significant set bit, using only bitwise operators

  4. 4

    How to clear most significant bit in byte?

  5. 5

    Comparing the Most Significant Bit of two numbers: ==, <, <=

  6. 6

    Get the most significant bit from an 8-bit value

  7. 7

    Get the most significant bit from an 8-bit value

  8. 8

    Index of second least significant set bit

  9. 9

    Efficient least significant set bit of "biginteger" class

  10. 10

    how is the most significant bit radix sort more efficient than the least significant bit radix sort?

  11. 11

    The MSB (most significant bit ) in pyserial python sent to arduino uno in damged

  12. 12

    How do I flip the most significant bit in MIPS?

  13. 13

    How to get position of right most set bit in C

  14. 14

    Writing a bit reader in JAVA (32-bit little-endian most-to-least-significant bit packing)

  15. 15

    Fastest way to get last significant bit position in a ulong (C#)?

  16. 16

    How to write a constant time function to copy the most significant bit to all bits

  17. 17

    How to change the most significant bit to a 1 after shifting an int to the right using '>>'?

  18. 18

    How to interpret using Most Significant Bit of single byte - Pcapng time format "if_tsresol"

  19. 19

    Most Significant Byte Calculation

  20. 20

    Hex of most significant nibble

  21. 21

    Xor starting with the significant bit

  22. 22

    Clearing least significant bit

  23. 23

    Least significant bit mips

  24. 24

    C set a bit (bit manipulation)

  25. 25

    Why is the segfault address NULL when accessing memory that has any of the 16 most significant bits set?

  26. 26

    Get most significant digit in python

  27. 27

    Getting least significant bit in JavaScript

  28. 28

    Extraction of the least significant bit of a pixel

  29. 29

    How to extract digits from a number in C? Begining from the most significant digit?

HotTag

Archive