bitwise-operators

What is (x & 1) and (x >>= 1)?

我只是一个虾纸丫 提交于 2020-08-17 12:41:26
问题 I am trying to do assignment: "Find the number of bits in an unsigned integer data type without using the sizeof() function." And my design is to convert the integer to bits and then to count them. For ex: 10 is 1010 and 5 is 101 Converting integer to a bit representation shows something like this: do { Vec.push_back( x & 1 ) } while ( x >>= 1 ); I don't want to just copy paste stuff. When I use F-10 I see what (x & 1) is doing but I don't know it is name or how it does its job(compare

What are 0x01 and 0x80 representative of in C bitwise operations?

一曲冷凌霜 提交于 2020-08-02 08:16:16
问题 I'm trying to reverse the order of bits in C (homework question, subject: bitwise operators). I found this solution, but I'm a little confused by the hex values used -- 0x01 and 0x80. unsigned char reverse(unsigned char c) { int shift; unsigned char result = 0; for (shift = 0; shift < CHAR_BITS; shift++) { if (c & (0x01 << shift)) result |= (0x80 >> shift); } return result; } The book I'm working out of hasn't discussed these kinds of values, so I'm not really sure what to make of them. Can

What are 0x01 and 0x80 representative of in C bitwise operations?

孤者浪人 提交于 2020-08-02 08:14:48
问题 I'm trying to reverse the order of bits in C (homework question, subject: bitwise operators). I found this solution, but I'm a little confused by the hex values used -- 0x01 and 0x80. unsigned char reverse(unsigned char c) { int shift; unsigned char result = 0; for (shift = 0; shift < CHAR_BITS; shift++) { if (c & (0x01 << shift)) result |= (0x80 >> shift); } return result; } The book I'm working out of hasn't discussed these kinds of values, so I'm not really sure what to make of them. Can

Converting unsigned char * to hexstring

家住魔仙堡 提交于 2020-07-21 07:35:46
问题 Below code takes a hex string(every byte is represented as its corresponidng hex value) converts it to unsigned char * buffer and then converts back to hex string. This code is testing the conversion from unsigned char* buffer to hex string which I need to send over the network to a receiver process. I chose hex string as unsigned char can be in range of 0 to 255 and there is no printable character after 127. The below code just tells the portion that bugs me. Its in the comment. #include

Leetcode Single Number II Operator Solution Explanation [closed]

岁酱吖の 提交于 2020-07-07 09:46:39
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 1 hour ago . Improve this question The question is ' Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one. ' I came up with a simple solution but found this solution online and was confused. Can someone explain