bit-shift

Am I extracting these fields correctly using bitwise shift? (tag, index, offset)

纵饮孤独 提交于 2019-12-24 09:33:37
问题 I am building a CPU cache emulator in C. I was hoping you could tell me if I am extracting these fields correctly: The 32-bit address should be broken up as follows: +---------------------------------------------------+ | tag (20 bits) | index (10 bits) | offset (2 bits) | +---------------------------------------------------+ Here is my code to obtain the values for each: void extract_fields(unsigned int address){ unsigned int tag, index, offset; // Extract tag tag = address >> 12; // Extract

C shift right not working correctly on int types in my program

本小妞迷上赌 提交于 2019-12-24 08:19:19
问题 I have the following function in C: int lrot32(int a, int n) { printf("%X SHR %d = %X\n",a, 32-n, (a >> (32-n))); return ((a << n) | (a >> (32-n))); } When I pass as arguments lrot32(0x8F5AEB9C, 0xB) I get the following: 8F5AEB9C shr 21 = FFFFFC7A However, the result should be 47A. What am I doing wrong? Thank you for your time 回答1: int is a signed integer type. C11 6.5.7p4-5 says the following: 4 The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros.

How do I properly loop through and print bits of an Int, Long, Float, or BigInteger?

血红的双手。 提交于 2019-12-24 08:15:47
问题 I'm trying to debug some bit shifting operations and I need to visualize the bits as they exist before and after a Bit-Shifting operation. I read from this answer that I may need to handle backfill from the shifting, but I'm not sure what that means. I think that by asking this question (how do I print the bits in a int) I can figure out what the backfill is, and perhaps some other questions I have. Here is my sample code so far. static string GetBits(int num) { StringBuilder sb = new

Is there a difference between bitshift by a constant versus bitshifting by a variable with the same value, in C?

 ̄綄美尐妖づ 提交于 2019-12-24 03:35:24
问题 I'm attempting to bitshift the value 0xFFFFFFFF by 32 bits, and it correctly comes to 0 if I write x = x << 32; however it stays as 0xFFFFFFFF when I write: x = x << y when y = 32 I really don't understand this at all. I need to be able to use a variable though, for a function that shifts by 32 - n Edit If << 32 is undefined, then I really can't perceive a way to create a function that pads n - upper bits with 1's 回答1: It is undefined behavior to shift by the bit length of a variable or

Undefined behavior when constexpr-evaluating negative bitshift?

吃可爱长大的小学妹 提交于 2019-12-23 20:51:11
问题 Consider the following snippet of code: int main(){ constexpr int x = -1; if(x >= 0){ constexpr int y = 1<<x; } } GCC 7 (and probably other versions of GCC) refuses to compile this and says: error: right operand of shift expression '(1 << -1)' is negative [-fpermissive] I can guess where this may have come from: the constexpr declaration on y makes GCC evaluate y at compile time, where it might be negative. Removing the constexpr fixes the error. However, is this undefined behavior by the

How do I perform a proper unsigned right shift in PHP?

拟墨画扇 提交于 2019-12-23 18:54:38
问题 Is this possible to get the same results in PHP and Javascript? Example: Javascript <script> function urshift(a, b) { return a >>> b; } document.write(urshift(10,3)+"<br />"); document.write(urshift(-10,3)+"<br />"); document.write(urshift(33, 33)+"<br />"); document.write(urshift(-10, -30)+"<br />"); document.write(urshift(-14, 5)+"<br />"); </script> output: 1 536870910 16 1073741821 134217727 PHP function uRShift($a, $b) { if ($a < 0) { $a = ($a >> 1); $a &= 2147483647; $a |= 0x40000000;

Java shift operator

感情迁移 提交于 2019-12-23 14:39:00
问题 Consider the following Java code: byte a = -64; System.out.println(a << 1); The output of this code is -128 I tried as follows to figure out why this is the output: 64 = 0 1000000 (the MSB is the sign bit) -64= 1 1000000 (Tow's complement format) Expected output after shifting: 1 0000000 (This is equal to 0, because the MSB is just a sign bit) Please anyone explain what I am missing. 回答1: The two's complement representation of -128 is 10000000, thus your results are correct. 回答2: 10000000 is

Bit shifting a character with wrap? C++

倖福魔咒の 提交于 2019-12-23 09:12:59
问题 I have a binary file that will be read in as characters. Each character was bit shifted to the left unknown number of times (assuming with wrap) by someone else. I want to be able to read in each character and then wrap shift to the right (the number of times to shift I guess will have to be figured out manually, because I haven't figured out another way). So, my current idea is that I read in a character, create a copy with temp and then use XOR: char letter; //will hold the read in letter

What is the fastest way to find integer square root using bit shifts?

只愿长相守 提交于 2019-12-23 09:01:14
问题 I was looking for the fastest method to calculate the square root(integer) of a number(integer). I came across this solution in wikipedia which finds the square root of a number(if its a perfect square) or the square root of its nearest lower perfect square (if the given number is not a perfect square: short isqrt(short num) { short res = 0; short bit = 1 << 14; // The second-to-top bit is set: 1L<<30 for long // "bit" starts at the highest power of four <= the argument. while (bit > num) bit

Use Perl to Add GIF Image Other Than 8-bit to PDF

邮差的信 提交于 2019-12-23 02:08:53
问题 I am attempting to add non-interlaced GIF images other than 8-bit to a PDF document without having to fully decode the bitstream using PDF::Create for Perl. The LZWDecode algorithm that is part of the PDF standard requires all images to have a minimum LZW code size of 8-bits, and PDF::Create is hard-coded to only embed 8-bit images. So far, I have adapted the image loader from PDF::Create to read a 5-bit image and to fully decode the LZW stream. I am then able to use the encoder algorithm