std-bitset

in bitset, can i use “to_ulong” for a specific range of bits?

断了今生、忘了曾经 提交于 2019-11-28 12:01:12
hi im working on something that demands me to get access to specific/range of bits. i decided to use bitset because it is easy to get access to specific bits but can i extract a whole range of bits? Method A: return (the_bitset >> start_bit).to_ulong(); Method B (faster than method A by 100 times on my machine): unsigned long mask = 1; unsigned long result = 0; for (size_t i = start_bit; i < end_bit; ++ i) { if (the_bitset.test(i)) result |= mask; mask <<= 1; } return result; 来源: https://stackoverflow.com/questions/2177186/in-bitset-can-i-use-to-ulong-for-a-specific-range-of-bits

in bitset, can i use “to_ulong” for a specific range of bits?

佐手、 提交于 2019-11-27 06:41:02
问题 hi im working on something that demands me to get access to specific/range of bits. i decided to use bitset because it is easy to get access to specific bits but can i extract a whole range of bits? 回答1: Method A: return (the_bitset >> start_bit).to_ulong(); Method B (faster than method A by 100 times on my machine): unsigned long mask = 1; unsigned long result = 0; for (size_t i = start_bit; i < end_bit; ++ i) { if (the_bitset.test(i)) result |= mask; mask <<= 1; } return result; 来源: https:/

How to print (using cout) a number in binary form?

一个人想着一个人 提交于 2019-11-26 00:57:09
问题 I\'m following a college course about operating systems and we\'re learning how to convert from binary to hexadecimal, decimal to hexadecimal, etc. and today we just learned how signed/unsigned numbers are stored in memory using the two\'s complement (~number + 1). We have a couple of exercises to do on paper and I would like to be able to verify my answers before submitting my work to the teacher. I wrote a C++ program for the first few exercises but now I\'m stuck as to how I could verify

How to print (using cout) a number in binary form?

孤街浪徒 提交于 2019-11-25 20:21:26
I'm following a college course about operating systems and we're learning how to convert from binary to hexadecimal, decimal to hexadecimal, etc. and today we just learned how signed/unsigned numbers are stored in memory using the two's complement (~number + 1). We have a couple of exercises to do on paper and I would like to be able to verify my answers before submitting my work to the teacher. I wrote a C++ program for the first few exercises but now I'm stuck as to how I could verify my answer with the following problem: char a, b; short c; a = -58; c = -315; b = a >> 3; and we need to show