bit-shift

Converting from 8 bits to 1 byte

不想你离开。 提交于 2019-12-07 09:23:29
问题 I have a string of 8 bits and I want to convert it into 1 byte. I am not sure why my function is not working properly. I have 8 bits stored into an array of 8 unsigned chars. This is my method so far: unsigned int bitsToBytes(unsigned char *bits) { unsigned int sum = 0; for(int i = 7; i >= 0; i--) { sum += bits[i]; sum<<=1; } return sum; } int main() { unsigned char bits[8]; unsigned int byt; byt = bitsToBytes(bits); cout << byt; //doesn't give me the right result } EDIT: My array of bits

Bitwise shift in C [duplicate]

こ雲淡風輕ζ 提交于 2019-12-07 08:12:39
问题 This question already has answers here : Weird behavior of right shift operator (1 >> 32) (7 answers) Closed 2 years ago . I got some C code that confuses me: int a = 1; int b = 32; printf("%d\n %d\n", a<<b, 1<<32); The output is 1 0 The code was run on Ubuntu 16.04 (Xenial Xerus), and I compiled it using gcc -m32 a.c with GCC version 5.4.0. I've read some posts that have explained why a<<b outputs 1, but I don't understand why 1<<32 results to 0. I mean, what is the difference between a<<b

Java bitshift strangeness

匆匆过客 提交于 2019-12-07 04:58:50
问题 Java has 2 bitshift operators for right shifts: >> shifts right, and is dependant on the sign bit for the sign of the result >>> shifts right and shifts a zero into leftmost bits http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op3.html This seems fairly simple, so can anyone explain to me why this code, when given a value of -128 for bar, produces a value of -2 for foo: byte foo = (byte)((bar & ((byte)-64)) >>> 6); What this is meant to do is take an 8bit byte, mask of the leftmost

c standard and bitshifts

浪尽此生 提交于 2019-12-07 04:49:54
问题 This question was first inspired by the (unexpected) results of this code: uint16_t t16 = 0; uint8_t t8 = 0x80; uint8_t t8_res; t16 = (t8 << 1); t8_res = (t8 << 1); printf("t16: %x\n", t16); // Expect 0, get 0x100 printf(" t8: %x\n", t8_res); // Expect 0, get 0 But it turns out this makes sense: 6.5.7 Bitwise shift operators Constraints 2 Each of the operands shall have integer type Thus the originally confused line is equivalent to: t16 = (uint16_t) (((int) t8) << 1); A little non-intuitive

What is the most efficient way to enumerate vertices of k dimensional hypercube in C++?

给你一囗甜甜゛ 提交于 2019-12-07 04:44:53
问题 Basic Question: I have a k dimensional box. I have a vector of upper bounds and lower bounds. What is the most efficient way to enumerate the coordinates of the vertices? Background: As an example, say I have a 3 dimensional box. What is the most efficient algorithm / code to obtain: vertex[0] = ( 0, 0, 0 ) -> ( L_0, L_1, L_2 ) vertex[1] = ( 0, 0, 1 ) -> ( L_0, L_1, U_2 ) vertex[2] = ( 0, 1, 0 ) -> ( L_0, U_1, L_2 ) vertex[3] = ( 0, 1, 1 ) -> ( L_0, U_1, U_2 ) vertex[4] = ( 1, 0, 0 ) -> ( U_0

Compute signed long max value in C using bit shift

陌路散爱 提交于 2019-12-07 02:57:25
Just started learning C yesterday and this is about to drive me crazy on the new year's day... Try to print the different int ranges using bit shift operations. Everything works fine apart from the signed long max/min value. Can't figure out why (1 << 63) - 1 returns -1 ? But (1 << 64) -1 for unsigned long long works fine... #include <limits.h> #include <stdio.h> void print_range() { signed char scmax = (1 << 7) - 1; char c = scmax; // char means signed char! unsigned char uscmax = (1 << 8) - 1; char cmin = -(1 << 7); unsigned char ucmin = 0; printf("signed char max: %d = %d, unsigned char max

Removing bit at specific index

喜欢而已 提交于 2019-12-07 01:13:54
问题 I'm basically trying to remove a bit from an integer at a specific index. That is, I do not want to unset/clear the bit; I actually want to strip it, so that every higher bit moves down, replacing the respective bit at its position. Visually, this could be compared to deleting an element from an array or removing a character from a string. For the sake of clarity, some examples: 1011011 (original number) ^ index = 2 0101111 (result) 10000000000000000000000000000001 ^ index = 31

Converting to Binary using bitwise and bitshift

∥☆過路亽.° 提交于 2019-12-06 16:49:19
I am trying to create a function to print a number in binary using bitwise and bit shifting but I am having trouble printing it correctly. The following is my code. void PrintInBinary( unsigned int decNum ) { int i = 0; unsigned int highestOne = 1 << (sizeof(unsigned int)*8 - 1); for( i = 0; i < sizeof(int)*8; i++ ) { printf( "%u", decNum & (highestOne >> i) ); } printf("\n"); } int main() { unsigned int a = 128; PrintInBinary( a ); system("PAUSE"); return 0; } The following is the output: 0000000000000000000000001280000000 Basically, its printing the 2^bit rather than just a 1 at each bit

Undoing shifts without truncating

点点圈 提交于 2019-12-06 15:15:30
I'm a bit baffled by this. Shouldn't the values truncate after the shift? Does anyone know why this happens? long a, b, c, n; //assign any value to a, set b and c to 0x000...0 n = 128; //any number works; b = a << n; c = b >> n; a == (b >> n); // True a == c; //True; Postscript I've always understood that if you shift a buffer in any direction, the values that "fall" outside the buffer size get truncated, and that they're essentially lost unless you get them from the original buffer. Thats been true for every platform I've ever worked with in assembly, and I thought that'd extend to the higher

Multiplication using Logical shifts in MIPS assembly

狂风中的少年 提交于 2019-12-06 11:49:20
Can someone please give me pointers on how I can go about making a code that multiplies using shifts in MIPS assembly? I don't understand how having a number 2^n can help me multiply using an odd multiplicand I currently have this code, I'm trying to make a calculator .text li $v0, 4 la $a0, ask_1 syscall li $v0,5 syscall move $s1, $v0 li $v0, 4 la $a0, ask_2 syscall li $v0,5 syscall move $s2, $v0 #sll $s2, $s2, 3 #$s2 * $s2^3 = result srl $s2, $s2, 1 li $v0, 1 la $a0, ($s2) syscall .data ask_1: .asciiz "Enter Multiplier\n" ask_2: .asciiz "Enter Multiplicand\n" result: .asciiz "The Answer is: