bit-manipulation

Extend Enum with flag methods?

允我心安 提交于 2019-12-10 11:11:11
问题 I have found good examples on how to create extension methods to read out single values from bitwise enums. But now that C# 4 has added the HasFlag method they are really not needed. What I think would be really helpful though is an extension to SET a single flag! I have many situations where I need to set the flag values individually. I want an extension method with this signature: enumVariable.SetFlag(EnumType.SingleFlag, true); OR possibly: enumVariable.SetFlag<EnumType>(EnumType

Minimum number of bits are to represent a given `int`?

守給你的承諾、 提交于 2019-12-10 10:38:28
问题 In C++, what's the fastest way to find out how many bits are needed to store a given int? I can try dividing the number with 2 many times but divisions are pretty slow. Is there any fast way? edit: Thanks a lot for the answers guys. When I say an int my post, I mean any 4 byte int. For example, if I store 30665, I want to get as a result 15 bits. 回答1: You can break the value progressively by halves to narrow it down faster. int bits_needed(uint32_t value) { int bits = 0; if (value >= 0x10000)

Reversing a Number using bitwise shift

荒凉一梦 提交于 2019-12-10 09:53:16
问题 I am trying to find a way to reverse a number without Converting it to a string to find the length Reversing the string and parsing it back Running a separate loop to compute the Length i am currently doing it this way public static int getReverse(int num){ int revnum =0; for( int i = Integer.toString(num).length() - 1 ; num>0 ; i-- ){ revnum += num % 10 * Math.pow( 10 , i ); num /= 10; } return revnum; } But I would Like to implement the above 3 conditions. I am looking for a way , possibly

Why aren't bitwise operators as smart as logical “and\or” operators

随声附和 提交于 2019-12-10 05:16:37
问题 I just noticed that bitwise operations aren't as "smart" as logical "and\or" operations and I wonder why? Here's an example: // For the record private bool getTrue(){return true;} private bool getFalse(){return false;} // Since a is true it wont enter getFalse. bool a = getTrue() || getFalse(); // Since a is false it wont enter getTrue. bool b = getFalse() && getTrue(); // Since b is false it wont enter getTrue. b = b && getTrue(); However the bitwise operators "|=" and "&=" aren't as smart:

Concatenate two 32 bit int to get a 64 bit long in Python

▼魔方 西西 提交于 2019-12-10 04:44:34
问题 I want to generate 64 bits long int to serve as unique ID's for documents. One idea is to combine the user's ID, which is a 32 bit int, with the Unix timestamp, which is another 32 bits int, to form an unique 64 bits long integer. A scaled-down example would be: Combine two 4-bit numbers 0010 and 0101 to form the 8-bit number 00100101 . Does this scheme make sense? If it does, how do I do the "concatenation" of numbers in Python? 回答1: Left shift the first number by the number of bits in the

Why result of unsigned char << unsigned char is not unsigned char

江枫思渺然 提交于 2019-12-10 04:01:55
问题 I'm getting results from left shift to which I could not find an explanation. unsigned char value = 0xff; // 1111 1111 unsigned char = 0x01; // 0000 0001 std::cout << "SIZEOF value " << sizeof(value) << "\n"; // prints 1 as expected std::cout << "SIZEOF shift " << sizeof(shift) << "\n"; // prints 1 as expected std::cout << "result " << (value << shift) << "\n"; // prints 510 ??? std::cout << "SIZEOF result " << sizeof(value << shift) << "\n"; // prints 4 ??? I was expecting result to be 1111

Optimize an array of tribools for space

你说的曾经没有我的故事 提交于 2019-12-10 03:07:06
问题 Let me start with some background: By "tribool" I understand a variable which can hold one of the following values: true , false or null . In question Copying array of ints vs pointers to bools , the OP wanted to have an array of tribools (more or less) which would be as small as possible. With "a bit of" most basic bit-fu I came up a solution which used 2 bits per tribool and allowed to store the OP's array of 64 tribools in 16 bytes, which is OK. The tribool mechanics I used were simple,

Java enum confusion with creating a bitmask and checking permissions

回眸只為那壹抹淺笑 提交于 2019-12-10 03:04:06
问题 I want to port this c# permission module to java, but I am confused how I can do this when I can't save the numeric value in the database and then cast it to the enumeration representation. In c#, I create a enum like this: public enum ArticlePermission { CanRead = 1, CanWrite = 2, CanDelete = 4, CanMove = 16 } I then can create a permission set like: ArticlePermission johnsArticlePermission = ArticlePermission.CanRead | ArticlePermission.CanMove; I then save this into the database using:

Why does golang RGBA.RGBA() method use | and <<?

江枫思渺然 提交于 2019-12-10 02:34:53
问题 In the golang color package, there is a method to get r,g,b,a values from an RGBA object: func (c RGBA) RGBA() (r, g, b, a uint32) { r = uint32(c.R) r |= r << 8 g = uint32(c.G) g |= g << 8 b = uint32(c.B) b |= b << 8 a = uint32(c.A) a |= a << 8 return } If I were to implement this simple function, I would just write this func (c RGBA) RGBA() (r, g, b, a uint32) { r = uint32(c.R) g = uint32(c.G) b = uint32(c.B) a = uint32(c.A) return } What's the reason r |= r << 8 is used? 回答1: From the the

Bitwise operations with big integers

空扰寡人 提交于 2019-12-10 02:00:40
问题 I am implementing decoding of BER-compressed integers and recently I've found a weird JavaScript behavior related to bitwise operations with big integers. E.g.: var a = 17516032; // has 25 bits alert(a << 7) // outputs -2052915200 alert(a * 128) // outputs 2242052096 alert(2242052096 >> 16) // outputs -31325 alert(2242052096 / 65536) // outputs 34211 While the first workaround (multiplication instead of left shift) is acceptable, the second isn't. Why it happens? How to bear with it? 回答1: