bit-manipulation

Simulate a 128-bit unsigned integer in SQL and C# using a 64-bit signed value?

只愿长相守 提交于 2019-12-06 16:21:37
Take this scenario: You have a few flag enumerations in C# tied to (and in fact generated from) Enum-ish tables in SQL Server. Say you are a distributor, and you allow your resellers to specify what US states they ship to. Being a brilliant and elegant software engineer, you implemented these as a bitwise-combinable flag value to save storage: create table USState ( StateID bigint, StateAbbr char(2), StateName varchar(50)) /* insert all US States + DC into USState, StateIDs must be in powers of two */ /* StateID 0 reserved for 'None': */ create procedure GetStatesByFlag (@StateFlags bigint) as

How to use bitwise operators to return a 0 or 1

时光总嘲笑我的痴心妄想 提交于 2019-12-06 16:17:56
问题 My function takes in a 32 bit int and I need to return a 0 or 1 if that number has a 1 in any even position. I cant use any conditional statements I also can only access 8 bits at a time. Here is an example input: 10001000 01011101 00000000 11001110 1) Shift the bits and and them with AA(10101010) and store each one in a variable. int a = 10001000 int b = 1000 int c = 0 int d = 10001010 Now I need to return a 0 if there were no odd bits set and 1 if there were. As we can see there were. So I

How can I create a 48-bit uint for bit mask

戏子无情 提交于 2019-12-06 15:16:05
问题 I am trying to create a 48-bit integer value. I understand it may be possible to use a char array or struct, but I want to be able to do bit masking/manipulation and I'm not sure how that can be done. Currently the program uses a 16-bit uint and I need to change it to 48. It is a bytecode interpreter and I want to expand the memory addressing to 4GB. I could just use 64-bit, but that would waste a lot of space. Here is a sample of the code: unsigned int program[] = { 0x1064, 0x11C8, 0x2201,

c++ bitstring to byte

匆匆过客 提交于 2019-12-06 14:51:52
问题 For an assignment, I'm doing a compression/decompression of Huffman algorithm in Visual Studio. After I get the 8 bits ( 10101010 for example) I want to convert it to a byte. This is the code I have: unsigned byte = 0; string stringof8 = "11100011"; for (unsigned b = 0; b != 8; b++){ if (b < stringof8.length()) byte |= (stringof8[b] & 1) << b; } outf.put(byte); First couple of bitstring are output correctly as a byte but then if I have more than 3 bytes being pushed I get the same byte

Tilde C unsigned vs signed integer

荒凉一梦 提交于 2019-12-06 13:06:05
For example: unsigned int i = ~0; Result : Max number I can assign to i and signed int y = ~0; Result : -1 Why do I get -1 ? Shouldn't I get the maximum number that I can assign to y ? Both 4294967295 (a.k.a. UINT_MAX ) and -1 have the same binary representation of 0xFFFFFFFF or 32 bits all set to 1 . This is because signed numbers are represented using two's complement . A negative number has its MSB (most significant bit) set to 1 and its value determined by flipping the rest of the bits, adding 1 and multiplying by -1 . So if you have the MSB set to 1 and the rest of the bits also set to 1

How to output the Binary value of a variable in C++

南笙酒味 提交于 2019-12-06 12:47:37
问题 I've got a homework assignment in my C++ programming class to write a function that outputs the binary value of a variable's value. So for example, if I set a value of "a" to a char I should get the binary value of "a" output. My C++ professor isn't the greatest in the whole world and I'm having trouble getting my code to work using the cryptic examples he gave us. Right now, my code just outputs a binary value of 11111111 no matter what I set it too (unless its NULL then I get 00000000).

Variable bit shift

↘锁芯ラ 提交于 2019-12-06 11:48:44
问题 I'm looking to shift a register bits n times to the left specified with another register. For example, something like this shl eax, ebx The only problem being is that the 2nd operand cannot be a register, it has to be an immediate value. How would I be able to shift a register by a value not known at assemble-time? 回答1: In x86 you can specify a shift count in register cl . So shl eax, cl is valid. mov ecx, ebx shl eax, cl Note that you can only use the cl register, not eax or any other

Package for fast determination of similarity between two bit sequences

不问归期 提交于 2019-12-06 11:20:50
I need to compare a query bit sequence with a database of up to a million bit sequences. All bit sequences are 100 bits long. I need the lookup to be as fast as possible. Are there any packages out there for fast determination of the similarity between two bit sequences? --Edit-- The bit sequences are position sensitive. I have seen a possible algorithm on Bit Twiddling Hacks but if there is a ready made package that would be better. If the database is rather static, you may want to build a tree data structure on it. Search the tree recursively or in multiple threads and per search keep an

Increase set of numbers so that XOR sum is 0

我的梦境 提交于 2019-12-06 10:26:22
问题 I need some help with a problem that I have reduced to the following. I have N 30 bit numbers, such that the combined XOR of all of them is non-zero. I need to add a non-negative (0 or more) value to each of the N numbers, such that the combined XOR of the new numbers becomes 0, under the constraint that the total addition value (not the number of additions) is minimized. For example, if I had numbers (01010) 2 , (01011) 2 and (01100) 2 as three numbers (N = 3). Then, their combined XOR is

Extend Enum with flag methods?

守給你的承諾、 提交于 2019-12-06 09:55:44
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.SingleFlag, true); Today I found a solution on http://hugoware.net/blog/enums-flags-and-csharp . Thanks Hugo!