bit-manipulation

Verilog signed vs unsigned samples and first

孤人 提交于 2019-12-04 12:18:09
问题 Assuming I have a register reg [15:0] my_reg , which contains a 16-bit signed sample: How do I convert the sample from signed to unsigned? I have read this Wikipedia article, and am aware of the 2-bit complement for signed numbers, but how do I perform this conversion in Verilog efficiently? (I don't know if my_reg is positive or negatve, and it changes in every clock cycle = I receive a new sample on every positive clock edge). The ultimate goal (to add a little bit of context) is to

Replace byte in a int

最后都变了- 提交于 2019-12-04 12:10:53
A int is composed of 4 bytes. How could I replace one of those 4 bytes with a new byte. In other words I am looking for a method: int ReplaceByte(int index, int value, byte replaceByte) { // implementation } for example if I have the value FFFFFFFF (-1) and I will like to replace the byte 0 with 0A (10) then I will call the method as: ReplaceByte(0,-1,10) and I will like that method to return me FFFFFF0A Do I have to convert the int to a byte array then replace the byte I want then convert back to an int? I am looking for an efficient way of doing this. We are creating a debugger like program

Determine the sign of a 32 bit int

微笑、不失礼 提交于 2019-12-04 12:05:07
问题 Using ONLY: ! ~ & ^ | + << >> NO LOOPS I need to determine the sign of a 32 bit integer and I need to return 1 if positive, 0 if 0 and -1 if negative. Any ideas? I first thought about shifting over 31 bits and then looking at that sign but that obviously wont work and now I am kind of stuck. 回答1: Try this: (x >> 31) | (((0 - x) >> 31) & 1) How about this: (x >> 31) | (((~x + 1) >> 31) & 1) EDIT 2: In response to issues (or rather nit-picking) raised in the comments... Assumptions for these

User role permissions for different modules using bitwise operators

断了今生、忘了曾经 提交于 2019-12-04 11:39:15
So I have an application that has several modules (think of modules as different pages), each module has a set of permissions; view, add, edit, delete I want each user role to have privileges for each module, for example Role A Permissions Module 1 -> view Module 2 -> add, edit Module 3 -> view, add, edit, delete etc. How can I design the database to support that and how would I go about implementing it using bitwise operators (or would there be a more efficient way for this particular case?) I already have the user, user_role and role tables but I'm unsure on how to design the Module table.

Check division by 3 with binary operations?

僤鯓⒐⒋嵵緔 提交于 2019-12-04 11:35:22
问题 I've read this interesting answer about " Checking if a number is divisible by 3 " Although the answer is in Java , it seems to work with other languages also. Obviously we can do : boolean canBeDevidedBy3 = (i % 3) == 0; But the interesting part was this other calculation : boolean canBeDevidedBy3 = ((int) (i * 0x55555556L >> 30) & 3) == 0; For simplicity : 0x55555556L = "1010101010101010101010101010110" Nb There's also another method to check it : One can determine if an integer is

Finding Bit Positions in an unsigned 32-bit integer

懵懂的女人 提交于 2019-12-04 11:19:46
问题 I think I might have been asleep in my CS class when they talked about Bit Positions, so I am hoping someone can lend a hand. I have a unsigned 32-bit integer (Lets use the value: 28) According to some documentation I am going over, the value of the integer contains flags specifying various things. Bit positions within the flag are numbered from 1 (low-order) to 32 (high-order). All undefined flag bits are reserved and must be set to 0. I have a Table that shows the meanings of the flags,

NASM: Count how many bits in a 32 Bit number are set to 1

六月ゝ 毕业季﹏ 提交于 2019-12-04 11:17:59
I have a 32 Bit number and want to count know how many bits are 1. I'm thinking of this pseudocode: mov eax, [number] while(eax != 0) { div eax, 2 if(edx == 1) { ecx++; } shr eax, 1 } Is there a more efficient way? I'm using NASM on a x86 processor. (I'm just beginning with assembler, so please do not tell me to use code from extern libraries, because I do not even know how to include them ;) ) (I just found How to count the number of set bits in a 32-bit integer? which also contains my solution. There are other solutions posted, but unfortunatly I can't seem to figure out, how I would write

Returning i-th combination of a bit array

放肆的年华 提交于 2019-12-04 10:15:42
Given a bit array of fixed length and the number of 0s and 1s it contains, how can I arrange all possible combinations such that returning the i-th combinations takes the least possible time? It is not important the order in which they are returned. Here is an example: array length = 6 number of 0s = 4 number of 1s = 2 possible combinations (6! / 4! / 2!) 000011 000101 000110 001001 001010 001100 010001 010010 010100 011000 100001 100010 100100 101000 110000 problem 1st combination = 000011 5th combination = 001010 9th combination = 010100 With a different arrangement such as 100001 100010

MySQL bitwise operations, bloom filter

馋奶兔 提交于 2019-12-04 09:25:22
问题 I'd like to implement a bloom filter using MySQL (other a suggested alternative). The problem is as follows: Suppose I have a table that stores 8 bit integers, with these following values: 1: 10011010 2: 00110101 3: 10010100 4: 00100110 5: 00111011 6: 01101010 I'd like to find all results that are bitwise AND to this: 00011000 The results should be rows 1 and 5. However, in my problem, they aren't 8 bit integers, but rather n-bit integers. How do I store this, and how do I query? Speed is key

How do you randomly zero a bit in an integer?

萝らか妹 提交于 2019-12-04 09:20:49
问题 Updated with newer answer and better test Let's say I have the number 382 which is 101111110. How could I randomly turn a bit which is not 0 to 0? The why; Since people ask me why, I simply need to do this, removing a bit from an integer. based on the answer here is the result(working one) I ran this using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Diagnostics; namespace ConsoleApplication1 { class Program { static Random random; static