I'm looking at some code which should be trivial -- but my math is failing me miserably here.
Here's a condition that checks if a number if a power of 2 using ... |
I've recently decided to undertake an SMS project for sending and receiving SMS though a mobile.
The data is sent in PDU format - I am required to change ASCII characters ... |
if i have int temp=(1<<31)>>31. How come the temp becomes -1?
how do i get around this problem?
thanks
|
I am new to using the PARI C library.
How can I perform bitwise ('and' or 'or') operations on the GEN type variables?
|
// PWM frequency:
// 0 - 48 kHz
// 1 - 12 kHz
// 2 - 3 kHz
enum { MOTOR_FREQUENCY = 1 };
// Configure Timer 2 w. 250x period.
T2CON = 1 << 2 ...
|
I'm very new to dealing with bits and have got stuck on the following warning when compiling:
7: warning: left shift count >= width of type
My line 7 looks like this
unsigned long ...
|
int x = 0;
x^=x || x++ || ++x;
and the answer for x at last is 3.
How to analysis this expression?
little confused about this.
Thanks a lot.
|
|
Hey I have been having trouble with a C program. The program I have to write simulates the operation of a VAX computer. I have to take in 2 variables x ... |
I'm working on an assignment and I can't figure out how to implement this. I have to make a function sadd(int x, int y) that returns the numbers added together unless ... |
I was reading some code today by someone I consider to be a reasonable programmer, and I noticed they used a =~0 to set a loop quit variable.
Is there any ... |
I have a sample problem with w=1, y=7, z=0, x = ~(w && y) | y; and the solution is x = -1, but I can't figure out why?
Here is my ... |
I am playing around with xor decoding via a small C file, and am running into issues with endianness ...I am a bit stuck on how to work around them. ... |
Apologies if I am asking a very trivial question... But I couldn't find the answer for this.
I was reading Bit twiddling hacks page and it is using C in ... |
Please help me with this code. I don't know why the output is not 8.
#include <stdio.h>
#include <stdlib.h>
int main(){
char c;
c = c & 0;
...
|
Using ONLY
! ~ & ^ | +
How can I find out if a 32 bit number is TMax?
TMax is the maximum, two's complement number.
My thoughts so far have been:
int isTMax(int ...
|
In 3 operations how can I turn a byte into a 32 bit int that matches this:
0x1fffe
I can only explicitly access a byte at a time thus I start with 0xFF ... |
Only using these operators:
! ~ & ^ | +
Also, only being allowed to have literals the size of a byte (8 bits in this case) such as 0xFF but not 0xFFCC.
I ... |
I suppose sizeof(char) is one byte. Then when I write following code,
#include<stdio.h>
int main(void)
{
...
|
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 ... |
In the context if what JosAH has said The following algotithm will perform the transformation also. 1. make the 4 LH bits equal to the LH hex numeral or letter. 2. make the next 4 bits equal the next hex numeral or letter 3. repeat 2 above until the hex integer is exhausted. e.g. convert 0x 5 d 2 3 f ... |
|
|
|
This is rather dependent on the CPU and the instruction set that it has. However in most instructions sets in my experience the shift (or rotate) instruction can perform multiple bit shifts in a single operation. Note however that shifting by 100 bits in C/C++ would be undefined behviour because that is outside the range of an integer (unless you happen ... |
I don't have too much experience with bitwise operations. Here is what I'm trying to accomplish. I have enum say: enum myEnum { BILL = 0, KATIE = 1, JOHN = 2 } Then I have an int that represents some combination of that enum, like: int WhoIsAllowed = BILL | KATIE | JOHN; Then I wanted to be able to ... |
Hi all, I'm trying to write a program that extracts the first k bits of 64-bit double values, given 2^k (which I've defined as size). Here, I use the example that k = 3. For example, 101001...011 would return 5, since the first 3 bits are 101. #include #include #define size 8 #define NBITS 64 // number of bits ... |
On Jul 15, 2:02 am, "Malcolm McLean" news:1184444850.478621.170190@22g2000hsm.googlegro ups.com...how can u find whether a no has consecutive zero bits either in from LSB or MSB. if they exist in between, then what to do? plz clarify. > Take the binary complement of the number (~). Take two copies, and shift one a bit (<<). Then ... |
Hi, I am confused regarding these concepts. Please clearify. 1. Why is bitwise copying unsafe? 2. Does copy constructor does bitwise copying? 3. Is the main use of explicit copy constructor is to go for deep copying? Thanks in advance. 1. bitwise copying will be unsafe, if you are putting some content on heap; I mean dynamically allocating memory and storing ... |
If both numbers a and b differ in only one bit then a^b must be a number x with only one bit set to one (1). Such a number definitely isn't equal to zero but the following expression is equal to zero: x&(x-1). For all other pairs of nunbers a and b the number x= a^b will be zero ot that ... |
|
Thank you Sir . I've got the desired output. Sir kindly consider the following question? int setbit(int val,int bitnum); This function should set the given bit ('bitnum') in the given value ('val') and should return that value.All other bits should not get affected.Note that first bit is given as 1 and last bit as 32. I AM UNABLE TO UNDERSTAND THE ... |
Consider the following code: #include int main(void) { unsigned char c = 0; unsigned int i = ~c; printf("i = %u\n", i); return 0; } I compiled and ran this with 32-bit VC++ and 32-bit gcc. In both cases, this code prints 4294967295(which is 2^32 - 1) - UINT_MAX. My doubt: since we are taking ~c, should't 255 be assigned to ... |
select the bit you want, say 3 bit (0100 = 0x04) AND the value with that bit and shift right by the number of 0 (2 in this case) bit = (number & 0x04) >> 2; bit will have either the value 0 or the value 1 depending on the value of the third bit in number. Preferably bitwise operations should ... |
ok I want to extract the first 4 bits of a 32 bit interger. The SIZE doesn't really matter. 8,16, 0r 32. I want the first 4 bits.....bit 0,1,2 and,3. I want to exract these and place into another variable. here is my Idea int bits1to3=(x>>1)&0x0f; // bits 1 2 3 4 or long bits1to3=(x>>12)&0x0f; // bits 12 13 14 15 ... |
ok here is the question. I want to exract the first 4 bits in a int so let say int b = somenumber; int result= 0; result = b << 4; if I got this right result should contain the 4 bits that were shifter to the left right? :) and if I'm wrong how can i get an x number ... |
The result of the AND operation will be an int, as normal. In C an int is "true" if it is non-zero and "false" if it is 0. So the (subtype & TT_VALUESVALID) will be evaluated, then the ! will invert the true/falseness. E.g. if the result of "subtype & TT_VALUESVALID" was 0x10000, 0x10000 is non zero so 'true', so !0x10000 ... |
37. Bitwise And cboard.cprogramming.com#include int display_binary(unsigned short number); int main() { unsigned short number; printf("Enter a whole number between 0 and 65,535: "); scanf("%hu", &number); printf("\n\n Decimal: %hu\n", number); printf("Hexadecimal: %x\n", number); display_binary(number); getch(); return 0; } int display_binary(unsigned short number) { unsigned short mask = 0x8000; int c, k; for(c = 0; c < 16; c++) { k = (number & mask); ... |
|
>I thought this forum is to share any C programming related topics..! It's most often used for Q&A. If you post random stuff that could easily be found with a web search, some people would see that as polluting the forum. I'd say that unsolicited sharing of knowledge isn't a bad thing, but keep in mind that each thread you create ... |
What is the type of your periphrials[ ]? Looking at your source material at the link given, it appears that the mouse bit is the most significant bit in the 28bit field. This is definitely not the (1<<0) bit. What it should be depends on the type of periphrials[ ]. Instead of an array, if perihrials was a 32-bit quantity (unsigned ... |
|
Bitwise method multiplication Ok, so I'm writing a program that multiplies two signed 32-bit integers. I must use the shift /add method to do this. And i must use this prototype long product = mult32(long multiplier, long multiplicand, int *status); Since, the product of two 32-bit numbers requires a 64-bit product to accommodate the largest possible value. In the prototype shown, ... |
Essentially I'm doing an assignment of a loop with 9973 integers in an array (closest prime to 10,000 so dual loops are needed), adding them up, and doing this 200,000 times. It's an assignment about unrolling and efficiency in building loops. Just to say, we're specifically not allowed to have the compiler optimize. All that said, we have different targets for ... |
|
I am preparing a program for 8bits bitwise rotation in which the input character will shift right by one bit everytime, eg. when input "aaa", the result will be "-80 88 44" And then I came out with the following and the result for inputing let say "aaa" will be "-80 -80 -80" and it waits for me to input another ... |
Hi, i need to implement bitwise rotation (8 bits) (to the right) and I prepare the following while((ch=getchar()) != '\n') a=(ch>>1)|(ch>>(8-1)); printf("%d ", a); however the result come out to be a bitwise shifting, not rotation. Also , only the first character is being scan . Could any prof. instruct me how shd I correct it. ThNks |
I'm having some problems with some concepts. Using no loops nor conditionals, I want to understand how to work with bitwise manipulation using only these operations: <<, >>, +, |, ^, &, ~, !, = Some things I wanted to do are: 1. Negate a number with int negate(int x), such as negate(4) returns -4 I'm not quite sure how to ... |
I'm having some problems in an assignment of mine. The assignment is as follows: with a limited number of legal operations, perform various functions. No conditionals (if's) nor loops are allowed at all. The legal operations are: <<, >>, +, |, ^, &, ~, !, = I managed to do some of them, but there are those that I am having ... |
A while back, I saw a function (meaning a single executable line, and NOT an algorithm), that somehow properly added two numbers and was apparently patented by IBM. Has anyone seen this? Does anyone happen to remember it? If anyone knows it, please post here. If anyone is wondering why I am not just adding instead...please don't post here :P |
Assuming put_zero is trying to clear the least significant bit, the constant value you're and-ing with is wrong. The constant you've written is the decimal number 11111110, not the binary value. The decimal value you want to use is 254 = binary 11111110. In decimal, the low 8 bits of 11111110 = b'11000110. 116 = b'01110100. Anding the two together gives ... |
okay, so I have no idea on how this would work. Im suppose to use convert binary to decimal then decimal to octal. i got the binary to decimal part but the decimal to octal part confuses me. the assignment says to "employ bit mask" integer which when ANDed with the original binary integer give you the corresponding octal bits) on ... |
|
I'm studying bit manipulation. My assignment is to write two functions. 1. convert string to all upper, 2. convert string to all lower. The trick is that I cannot use toupper or tolower. I have to use bit manipulation. I have read many articles and the chapters of various books on bitwise. It's very hard to find real world examples showing ... |
#include #include typedef struct { unsigned char b7:1; unsigned char b6:1; unsigned char b5:1; unsigned char b4:1; unsigned char b3:1; unsigned char b2:1; unsigned char b1:1; unsigned char b0:1; } _byte; union byte { unsigned char X; _byte B; }; char string[9]; char *byte2str (union byte A) { sprintf(string, "%d-%d-%d-%d-%d-%d-%d-%d",A.B.b0,A.B.b1,A.B.b2,A.B.b3,A.B.b4,A.B.b5,A.B.b6,A.B.b7); return string; } int main(int argc, char *argv[]) { ... |
|
|
I am having difficulty determining whether or not two addresses are on the same page. First let me define a page. A page is an area in memory that is 255 bytes. The first page (or called zero page) is in this range: 0x0000-0x00FF The second page would be in this range: 0x0100-0x01FF and so on... NOTE: addresses will range from ... |
I'm trying to optimize my code and I have two bitwise questions: I have a variable i. I am running a bitwise operation. If var1 = 0, then i = 1, if var1 = any other number, then i = 0. Would this work: i = ~(var1 & 0). Heres my second operation: I have a variable i. I am running ... |
#include int main () { int bytesw=0, unset; const int A=1,B=1<<1,C=1<<2; bytesw = bytesw | A; printf("A set: %d\n",bytesw); unset = ~ A; bytesw = bytesw & unset; printf("A unset: %d\n",bytesw); bytesw = bytesw | B; printf("B set: %d\n",bytesw); unset = ~ B; bytesw = bytesw & unset; printf("B unset: %d\n",bytesw); bytesw = bytesw | C; printf("C set: %d\n",bytesw); unset ... |
60. Bitwise Shift cboard.cprogramming.com#include int main( int argc, char *argv[] ){ int x = 1; unsigned int y = 1; int i; int limit = sizeof(int) * 8; printf("The value for x is %d. \n", x); printf("The value for y is %d. \n\n", y); printf("Signed \t\t Unsigned \n"); for(i = 0; i < limit - 1; i++){ x = x << 1; y ... |
i want to do stuffing... this works fine for only one charecter... if u find any flaws then please tell me where it is.... Code: #include #include #include main() { FILE *fp; unsigned char *ch; unsigned char *ch1; int one = 0x80; int counter = 0; int count = 8; int temp; int i = 0, j = ... |
|
> Hi, i want to use bitwise operations in my code to perform fast multiplications. Most modern compilers will generate the optimal shift/add sequences to replace common multiplications for you, without you having to obfuscate the code in the process. > so is it possible to perform bitwise multiplications between doubles? Nope (or rather with the kind of difficulty which doesn't ... |
> it just comes out -1. i don know why.... You need to use %u to correctly print the value of an unsigned int, not %d which is for (signed) int. Try that. Edit: The same goes for scanf(). Study carefully the conversion characters for printf() and scanf() for unsigned vs. signed types. |
Just for fun, I decided to try to implement addition on a purely bitwise level. I came up with this: Code: void main(void) { unsigned char num1 = 19; unsigned char num2 = 6; unsigned char sum = 0; unsigned char oldcarry = 0; unsigned char place = 1; unsigned char insertshift = 7; unsigned char x; for (x = 0; ... |
Hi, I have this piece of code, just testing the set and mask functions. Here I have a original hex string ABC and I want to put it into result in reverse. I have the mask as 0xF (1111) and take each letter at a time and set it in result, then I left shift the result var bits 4 places ... |
|
|
the program works nice. i didn't get any error messages when i used '20'. i'm using msvc++ (forgot ver #). after using 0x20, it works perfectly when using letters. with numbers or all other characters...that's my next task. i guess i have to use a function to check wheter than enter characters are between 65 and 90 and 97 to 122. ... |
70. bitwise help cboard.cprogramming.comAlmost everything about fields is implementation-dependent. Whether a field may overlap a word boundary is implementation-defined. Fields need not be named; unnamed fields (a colon and width only) are used for padding. The special width 0 may be used to force alignment at the next word boundary. Fields are assigned left to right on some machines and right to left on ... |
|
Code: #include #include char *bitdisp(char *dst, unsigned int value) { char *start = dst; unsigned int bit; for ( bit = ~(~0U >> 1) + 1; bit; bit >>= 1 ) { *dst++ = (value & bit) ? '1' : '0'; } *dst = '\0'; return start; } int main ( void ) { const char fmt[] = "The ... |
|
25. Which statement is true of the << operator? a) It is called the right shift operator. b) It is called the bitwise right shift operator. c) It shifts the bits of the second operand left by the number of bits specified by the first operand. d) It fills from the right with 0 bits. Ans: d) It fills from the ... |
|
|
|
for a start, if the number is 'unsigned' then the leftmost bit is not used to determine the sign of the integer, as it is unsigned. another thing to be cautious of, is that the size of an integer is variable depending on the hardware/compiler that you are using. the ANSI spec simply states that an integer value must be at ... |
Hi, I am trying to follow a book example on referencing union members and the method below has been used without explanation. Could anyone explain please? union u { char ch[2]; int num; }; int unioninit (union u val); /*declare function to return int*/ main() { union u val; int x; x = unioninit(val) /*this assigns values two chars to the ... |
Same as what adding, subtracting, multiplying, and dividing and modulo are for. Used the same way as well. They are binary operators. Do you understand the concepts of AND (&), OR (|), and XOR (^)? You just perform those operations on corresponding bits of the two operands, which is why it's called "bitwise". |
|
So I pass it a 16-bit instruction (x0111) and it should return the bits that I want (0000 XXX0 0000 0000). But it doesn't work. I've added in those printf's to do some debugging, and it seems like the bitwise AND operator is not doing what it should. Can anyone offer some advice? Example output: (instr=x0111) Temp(initial) = x0E00 Temp(ANDed) = ... |
People would definitely not ask such questions here because they are wise enough to understand that they possess enough intelligence to solve questions which require mental ability and thinking and not extensive coding / programming . Bitwise does not think little of the users of this forum, the post was to make them aware about it, as programming forums are used ... |
Did you initialise drun to zero? What does "I don't get any return if I print the variable drun." mean!? It has to have a value even if it is initialised or wrong. Why would you use the loop when it is so much less efficient? Your loop accesses buffer[0] to buffer[2], the original code used buffer[1] to buffer[3], so it ... |
There's nothing to it, really. It's just like doing those operations by hand the way one does in elementary school, the main difference being that you work in base 2, not base 10 (which actually makes it easier). With subtraction, there is a trick to reduce it to addition (look up "two's complement"). Division is the hardest, but long division is ... |
|
so i have to write a bitwise divide by power of two program (has to be bitwise because thats the only thing fast enough). assuming this was easy i tried Code: int foo(int x, int n){ x >> n; } and what a fool i turned out to be this doesn't work even remotely for negitive numbers. i'm not entirely sure ... |
I'm working on a bitwise problem where I can't use loops or logical operators. I need to return 1 ix x> My code that isn't working so far is: /* isLess - if x < y then return 1, else return 0 ... |
I am going to be using a C# application to connect to a database and look at user names, passwords, and permissions. Rather than have a different field in the database for each permission, I was hoping to use a combination of the following logic, but I am unsure of how to interpret it on the C# code side. For example... ... |
>Especially can somebody help me understand the " % 0x10" part? >Any help appreciated! Code: #include int main(void) { int i; for ( i = 0; i < 20; ++i ) { printf("%2d %% 0x10 == %2d\n", i, i % 0x10); } return 0; } /* my output 0 % 0x10 == 0 1 % 0x10 == 1 2 % ... |
Hello, here is my code that I came up with as a response to the chapter 2.9 exercises of "The C Programming Language". I am just now learning how binary and bitwise operations work. If you could give me tips or tell me how my code could be better optimized, I would appreciate it. Thanks. Code: // Bitwise exercises from "The ... |
|
I created a reverse polish notatino calculator, and it works great, but when I add bitwise operation, it doesn't compile. before using the bitwise operation I used all double variable, but when I changed it to int, so I can use the bitwise operation it changes my results, and if I add a bitwise operation I cannot compile. I'm posting the ... |
One caveat that gtpc should note is that the bit-wise binary operators are different from the relational operators. What I gave him applies to the testing of the bit flag with the short int. What Clifford gave him would be very applicable to the relational operations to which the results of the bit-wise testing would be input. BTW, long ago in ... |
|
|
#include #include int main() { unsigned short m1 = 0xf000, m2 = 0x0f00, m3 = 0x00f0, m4 = 0x000f; printf("%hu %hu %hu %hu\n", m1, m2, m3, m4); printf("%hx %hx %hx %hx\n", m1, m2, m3, m4); return 0; } /* my output 61440 3840 240 15 f000 f00 f0 f */ |
okay i understand all bitwise operators but in the chapter of k&r at bitwise operators i m really lost at there question i have no idea how to do them Exercise 2-6. Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged. ... |
sorry for mistake but my actually question is, i want to make one program binary no.s multiplication (using register method) i have to follow some rules, no1=1010 no2=0101 Rules are: store no1 in 8-bit register (like this way) no1=10100000 then have to perform shift left opration on it. (shift only one bit) so, no1=01000000 if shifted no. is 1 then add ... |
|