bit-manipulation

expressing hex value in 2's complement

两盒软妹~` 提交于 2020-01-14 09:22:49
问题 i have a string hex value, and i need to express it in 2's complement. string hx = "FF00"; what i did is, converting it to binary: string h = Convert.ToString(Convert.ToInt32(hx, 16), 2 ); then inverting it, but i couldn't use the NOT operator. is there any short way to invert the bits and then adding 1 (2's complement operation)? 回答1: The answer might depend on whether or not the bit width of the value is important to you. The short answer is: string hx = "FF00"; uint intVal = Convert

Minimum number of bytes that can contain an integer value

爱⌒轻易说出口 提交于 2020-01-14 05:24:05
问题 Given an integer value, I need some way to find out the minimum number of bytes needed to store the value. The value may be signed or unsigned, up to 64-bit . Also take the sign bit into account for signed integers. For example: 8 requires 1 byte at minimum unsigned 255 requires 1 byte at minimum signed 255 requires 2 bytes at minimum 4351 requires 2 bytes at minimum -4294967296 requires 5 bytes at minimum unsigned 0xFFFFFFFFFFFFFFFF requires 8 bytes at minimum I can think of a quick-and

C - Saturating Signed Integer Multiplication with Bitwise Operators

时光总嘲笑我的痴心妄想 提交于 2020-01-13 20:25:45
问题 Alright, so the assignment I have to do is to multiply a signed integer by 2 and return the value. If the value overflows then saturate it by returning Tmin or Tmax instead. The challenge is using only these logical operators (! ~ & ^ | + << >>) with no (if statements, loops, etc.) and only allowed a maximum of 20 logical operators. Now my thought process to tackle this problem was first to find the limits. So I divided Tmin/max by 2 to get the boundaries. Here's what I have: Positive This

Bitwise Float To Int

若如初见. 提交于 2020-01-13 17:57:11
问题 I am trying to figure out the algorithm to this but all I get with google is doing it with casting. I need to know the details. So if we have a float x and want to return its binary representation what do we need to do? I know we need to return the float if its NaN or a infinity but otherwise what are the steps? EDIT The function takes in an unsigned int, to be used as if it was a float, and then return the integer the number represents. I cannot use casting, just conditionals and bit-wise

Computing the floor of log₂(x) using only bitwise operators in C

隐身守侯 提交于 2020-01-13 12:23:16
问题 For homework, using C, I'm supposed to make a program that finds the log base 2 of a number greater than 0 using only the operators ! ~ & ^ | + << >> . I know that I'm supposed to shift right a number of times, but I don't know how to keep track of the number of times without having any loops or if s. I've been stuck on this question for days, so any help is appreciated. int ilog2(int x) { x = x | (x >> 1); x = x | (x >> 2); x = x | (x >> 4); x = x | (x >> 8); x = x | (x >> 16); } This is

Computing the floor of log₂(x) using only bitwise operators in C

本小妞迷上赌 提交于 2020-01-13 12:22:10
问题 For homework, using C, I'm supposed to make a program that finds the log base 2 of a number greater than 0 using only the operators ! ~ & ^ | + << >> . I know that I'm supposed to shift right a number of times, but I don't know how to keep track of the number of times without having any loops or if s. I've been stuck on this question for days, so any help is appreciated. int ilog2(int x) { x = x | (x >> 1); x = x | (x >> 2); x = x | (x >> 4); x = x | (x >> 8); x = x | (x >> 16); } This is

Computing the floor of log₂(x) using only bitwise operators in C

走远了吗. 提交于 2020-01-13 12:22:06
问题 For homework, using C, I'm supposed to make a program that finds the log base 2 of a number greater than 0 using only the operators ! ~ & ^ | + << >> . I know that I'm supposed to shift right a number of times, but I don't know how to keep track of the number of times without having any loops or if s. I've been stuck on this question for days, so any help is appreciated. int ilog2(int x) { x = x | (x >> 1); x = x | (x >> 2); x = x | (x >> 4); x = x | (x >> 8); x = x | (x >> 16); } This is

Create method which checks if x + y will overflow using bitwise operations

帅比萌擦擦* 提交于 2020-01-13 05:51:34
问题 I need to create a method in C using bitwise operations which checks if x + y will overflow or not. I can only use a maximum of 20 of the following operations; ! ~ & ^ | + << >> Keep in mind I have to test for both negative and positive numbers. I've tried several times to make it work. Is my logic sound? I'm going by: if (x + y) is less than x, then it has overflowed. Based on that logic, I wrote this; int addOK(int x, int y) { int sum = x + y; int nx = ((~x) + 1); int check = (sum + nx)>>31

Compare 64-bit integers by segments

天大地大妈咪最大 提交于 2020-01-13 02:54:07
问题 I have two 64-bit integers x and y . Each of them represents 5 short unsigned integers: the first 10 bits represent the first integer, the next 13 bits represent the 2nd integer, the next 16 bits represent the 3rd integer, the next 14 bits represent the 4th integer, and the rest bits represent the 5th integer. Let x0 , x1 , x2 , x3 , x4 be the 5 short integers that constitute x . Let y0 , y1 , y2 , y3 , y4 be the 5 short integers that constitute y . I need to know if x0 < y0 AND x1 < y1 AND

How to efficiently find the n-th set bit?

ぐ巨炮叔叔 提交于 2020-01-12 14:09:15
问题 For code related to this question, I need to compute the following as fast as possible: Given a 32 bit integer i , compute the position of the n -th least significant set bit. Both n and the result should be 0-indexed. For example, given the number i = 11010110101 2 and n = 4, the desired number is 7 as the fourth set bit is at position 7: 110 1 0110101. Using the pdep instruction from the BMI2 instruction set extension for x86 and the commonly available __builtin_ctz() intrinsic function,