完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
因此,我应该创建一个二进制转换的int代码,我有char的下面代码。我知道char是8位,int是16位,我需要更多的移位操作来检查“两个有多少个幂”,如果我有7个,我就有14个。有人能给我解释一下这些移位操作器是如何设置的。我知道他们正在检查两个的力量,但是他们是如何做到的,如果是的话,我如何将它应用到更高的比特上。如何设置移位操作符?使用SpkFlash和PIC18F45 25的SelLCD
以上来自于百度翻译 以下为原文 so I am supposed to create a code for binary conversion of int, I have the following code for char. I understand that char is 8 bits and int is 16 bits and I would need more shift operation to check " how many powers of two are there" if I have 7 here would I have 14. could someone explain to me how these shift operators are set up. I understand they are checking the power of two but how are they doing it and if yes how can I apply this to higher bits. how do i set up shift operators? Using serLCD by sparkfun and PIC18F4525 #include #include #include "configureUSART.h" // library for configureUSART(baud) #include "configuration_bits.h" void printCharAsBinary(unsigned char number); void WaitOneSecond(void); int main(void) { unsigned i = 0; configureUSART(9600ul, 1); // configure MCU serial communication module to run at 9600 baud // defined in configureUSART.c WaitOneSecond(); // The splash screen lasts about one second // LCD will not respond to printf() until it is finished. for(i=0; i < 256; i++) { printf("nr %u = ",i); printCharAsBinary((unsigned char)i); } while(1) { // MCUs run continuously so an endless loop is required. } } void printCharAsBinary(unsigned char number) { if ( ((number & 0b10000000) >> 7 ) == 1) printf("0b1"); else printf("0b0"); if ( ((number & 0b01000000) >> 6 ) == 1) printf("1"); else printf("0"); if ( ((number & 0b00100000) >> 5 ) == 1) printf("1"); else printf("0"); if ( ((number & 0b00010000) >> 4 ) == 1) printf("1"); else printf("0"); if ( ((number & 0b00001000) >> 3 ) == 1) printf("1"); else printf("0"); if ( ((number & 0b00000100) >> 2 ) == 1) printf("1"); else printf("0"); if ( ((number & 0b00000010) >> 1 ) == 1) printf("1"); else printf("0"); if ( ((number & 0b00000001) ) == 1) printf("1"); else printf("0"); } void WaitOneSecond(void) { int i = 0; for(i=0; i<=5; i++) { _delay(50000ul); // 50 000 * 4 / 1 000 000 = 1/5 s } } |
|
相关推荐
9个回答
|
|
你的理解是错误的,一个转变是一个分裂或乘以2。这对于数字的二进制表示具有特殊意义。例子。10001001右移(除以2)01000 100右移。00100010(除以2)注意CPU有特殊的硬件,你的移位实际上并没有划分。现在在你的代码中,如果你向左移动,看看高位,你可以使用一个for循环,而不是8到16的比较。注意,虽然int是CPU上的16位,但并非所有CPU都是这样。INT至少为16位。
以上来自于百度翻译 以下为原文 Your understanding is wrong a shift is a divide or multiply by 2. This has special meaning to a binary representation of a number. Example. 10001001 Shift right(divide by 2) 01000100 Shift right. 00100010(divide by 2) Note the CPU has special hardware your shift it that’s not actually divide. Now in your code if you shift left and look at the high bit you can use a for loop rather than 8 to 16 compares. Note while an int is 16 bits on you cpu, that is not true of all CPUs. An int is at least 16 bits. |
|
|
|
我有一种感觉,我认为这是错误的,但是当我问我的教授她刚刚说是的时候,我知道符号char范围从-128到127,而0b100000的十进制当量是128,现在考虑int的范围从-5到255.现在对右移和除法意味着什么?通过同样的逻辑是如何应用到unsindChar,理解这些问题中的一些似乎是相当基本的,但我试图更好地掌握这个主题。
以上来自于百度翻译 以下为原文 I had a feeling I was wrong to assume that it was the case however when I asked my prof she just said yes, I understand that signed char ranges from -128 to 127 and 0b10000000's decimal equivalent is 128 now to think about int they range from -256 to 255. now what does it mean to shift right and divide by 2. how is the same logic being applied to an unsigned char,understand that some of these question seem pretty basic but I am trying to grasp the subject a bit better |
|
|
|
我画了它,我想我有一个想法,假设这是一个0和1组成的字符列表。将其右移将意味着像从四到三的移位,这是数学除以2的等价物。
以上来自于百度翻译 以下为原文 I drew it and I think I have a thought _ _ _ _ _ _ _ _ let's assume this is a list of 0's and 1's that make up that char. shifting it right would mean something like shifting from position four to three,which is the mathematical equivalent of dividing by 2 |
|
|
|
符号数的移位是一个更复杂的问题。C没有给出确切的定义。结果将在不同编译器之间有所不同。我说右移相当于除以2。结果对于无符号数是相同的。
以上来自于百度翻译 以下为原文 Shifting of signed numbers is a more complicated subject. C does not give an exact definition of what that means. The result will vary between different compilers. I said a shift right is the equivalent of dividing by two. The result will be the same for an unsigned number. |
|
|
|
如果你在讨论有符号的字符,那么在XC8中,0B100000是-128,而不是+128No,一个已签名的int是16位,所以从0x7FFF=0B01111111111111= 327 67到0x8000=0B10000 000 000 000=-327 68。现在,右移和除以2.0x7FFF的右移为0B011111111111111=什么意思?0x3FFF=1638 3如果使用未签名的int,则0x8000右移变为0B0100000×0.00亿=0x4000=1638,但是对于签名的ItS0x8000右移变为=0B1100000亿兆=0xC000=-1638,但这是特定于XC8的。C标准没有定义如果你对一个已签名的变量做了正确的转换会发生什么,而不同的编译器会做不同的事情。
以上来自于百度翻译 以下为原文 If you are talking about signed chars, then 0b10000000 is -128, not +128 No. In XC8, a signed int is 16 bits, so ranges from 0x7FFF = 0b0111111111111111 = 32767 to 0x8000 = 0b1000000000000000 = -32768 now what does it mean to shift right and divide by 2. 0x7FFF shifted right becomes 0b0011111111111111 = 0x3FFF = 16383 if using unsigned ints, then 0x8000 shifted right becomes 0b0100000000000000 = 0x4000 = 16384 however for signed ints 0x8000 shifted right becomes= 0b1100000000000000 = 0xC000 = -16384 but this is specific to XC8. The C standard does NOT define what will happen if you do a right shift on a signed variable, and different compilers do it differently. |
|
|
|
“考虑int的范围从256到255。”我想你会发现它稍微多一些。签署- 32768到32767或无符号0到65535。
以上来自于百度翻译 以下为原文 "think about int they range from -256 to 255." I think you'll find it slightly more than that. signed -32768 to 32767 or unsigned 0 to 65535 |
|
|
|
|
|
|
|
您不需要那些& gt;& gt;and==,并且它将操作相同。
以上来自于百度翻译 以下为原文 You don't need those >> and ==, and it will operate the same. |
|
|
|
明确地说,Harry意味着可以简化,并且它的行为完全相同。也就是说,如果表达式的结果是非零,则将执行IF-()语句的正文。
以上来自于百度翻译 以下为原文 Agree. To be clear, Harry means that if ( ((number & 0b10000000) >> 7 ) == 1) can be simplified to if ( number & 0b10000000 ) and it will behave exactly the same. i.e. if the result of the expression is non-zero, the body of the if() statement will be executed. |
|
|
|
只有小组成员才能发言,加入小组>>
5171 浏览 9 评论
2001 浏览 8 评论
1931 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3176 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2228 浏览 5 评论
737浏览 1评论
622浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
509浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
635浏览 0评论
531浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-26 00:10 , Processed in 1.316117 second(s), Total 94, Slave 77 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号