bit-manipulation

C# EF Linq bitwise question

﹥>﹥吖頭↗ 提交于 2019-12-03 14:14:34
问题 Ok for example, I am using bitwise like such: Monday = 1, Tuesday = 2, Wednesday = 4, Thursday = 8 etc... I am using an Entity Framework class of Business. I am using a class and passing in a value like 7 (Monday, Tuesday, Wednesday). I want to return records that match any of those days public List<Business> GetBusinesses(long daysOfWeek) { using (var c = Context()) { return c.Businesses.Where(???).ToList(); } } Any help would be appreciated. Thanks! EDIT Ok, so I am attempting the following

K&R C Exercise Help

瘦欲@ 提交于 2019-12-03 13:55:11
I've been going through the K&R C Programming Language book and I'm stuck on Exercise 2-6 which reads: Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged. I'm having trouble understanding the exact thing they're looking for me to do. I looked at a possible answer here , but I still don't really understand. I think it's the wording that's throwing me off. Can anyone maybe explain what they're looking for me to do in a different way? I'm hoping that different wording will help me understand

Packing two shorts into one int, dealing with negative and positive

心已入冬 提交于 2019-12-03 13:50:35
I'm making a class PackedUnsigned1616 which stores two unsigned shorts in one int, and a class PackedSigned1616 which stores two signed shorts in one int. I've read up on bitwise operations, but I'm still confused on how to deal with signed and unsigned and values that are larger or smaller that a short's range (they are passed in as two ints). Here's what I've got so far: public final class PackedUnsigned1616 { public final int field; private static final int RIGHT = (2 << 15) - 1; private static final int LEFT = ((2 << 31) - 1) ^ RIGHT; public PackedUnsigned1616(int left, int right) { field

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

假如想象 提交于 2019-12-03 13:19:50
I'd like to write a function, in C, which takes the MSB of uint8_t , and if it's set, returns 0xFF and if not 0x00 . In short, which returns an integer where all the bits are set to the same value as the MSB. But I'd like to do it in a completely constant time way, no branches, no array offsets, just mathematical operations which are guaranteed to always touch the same number of bits. And ideally, without any undefined behavior. How can this be done? Ilmari Karonen How about: #define uint8_msb_to_all_bits(x) (0xFF * ((x) >> 7)) or even better: #define uint8_msb_to_all_bits(x) (-((x) >> 7)) The

Fast bit shift of a byte array - CMAC subkeys

断了今生、忘了曾经 提交于 2019-12-03 13:13:37
I need to implement as fast as possible left bit shift of a 16-byte array in JavaCard . I tried this code: private static final void rotateLeft(final byte[] output, final byte[] input) { short carry = 0; short i = (short) 16; do { --i; carry = (short)((input[i] << 1) | carry); output[i] = (byte)carry; carry = (short)((carry >> 8) & 1); } while (i > 0); } Any ideas how to improve the performace? I was thinking about some Util.getShort(...) and Util.setShort(...) magic, but I did not manage to make it work faster then the implementation above. This is one part of CMAC subkeys computation and it

Bit Shift and Bitwise operations to encode RGB values

こ雲淡風輕ζ 提交于 2019-12-03 12:52:48
问题 I would like to encode the RGB color to a single integer value. Let's say the algorithm for encoding is like this: int code = (blue * 256 * 256) + (green * 256) + red How can encode/decode RGB components to/from the code using bit shift and/or bitwise operators? 回答1: int blueMask = 0xFF0000, greenMask = 0xFF00, redMask = 0xFF; int r = 12, g = 13, b = 14; int bgrValue = (b << 16) + (g << 8) + r; System.out.println("blue:" + ((bgrValue & blueMask) >> 16)); System.out.println("red:" + ((bgrValue

Are there any good reasons to use bit shifting except for quick math?

↘锁芯ラ 提交于 2019-12-03 12:40:50
I understand bitwise operations and how they might be useful for different purposes, e.g. permissions. However, I don't seem to understand what use the bit shift operators are. I understand how they work, but I can't think of any scenarios where I might want to use them unless I want to do some really quick multiplication or division. Are there any other reasons to use bit-shifting? Guy Sirton There are many reasons, here are some: Let's say you represent a black and white image as a sequence of bits and you want to set a single pixel in this image generically. For example your byte offset may

C# Language: Changing the First Four Bits in a Byte

▼魔方 西西 提交于 2019-12-03 12:40:20
In order to utilize a byte to its fullest potential, I'm attempting to store two unique values into a byte: one in the first four bits and another in the second four bits. However, I've found that, while this practice allows for optimized memory allocation, it makes changing the individual values stored in the byte difficult. In my code, I want to change the first set of four bits in a byte while maintaining the value of the second four bits in the same byte. While bitwise operations allow me to easily retrieve and manipulate the first four bit values, I'm finding it difficult to concatenate

Java integer flag and bitwise operations for memory reduction

痴心易碎 提交于 2019-12-03 12:36:24
问题 Is using an integer flag and bitwise operations an effective way of reducing the memory footprint of high volume Objects? Memory Footprint It is my understanding that commonly a boolean is stored as an int in a JVM implementation. Is this correct? In which case surely the 32 flags represent a large memory footprint reduction. Although of course the JVM implementations vary, so this may not always be the case. Performance It is my understanding that CPUs are very number driven and bitwise

Converting a String representation of bits to a byte

自古美人都是妖i 提交于 2019-12-03 12:29:11
I'm just beginning to learn about file compression and I've run into a bit of a roadblock. I have an application that will encode a string such as "program" as a compressed binary representation "010100111111011000" (note this is still stored as a String). Encoding g 111 r 10 a 110 p 010 o 011 m 00 Now I need to write this to the file system using a FileOutputStream , the problem I'm having is, how can I convert the string "010100111111011000" to a byte[] / byte s to be written to the file system with FileOutputStream ? I've never worked with bits/bytes before so I'm kind of at a dead end here